Thumbnail not showing when using API to add image to library?

Hanh Dang asked on October 8, 2015 07:03

Hi all,

I create a function which used Kentico API to add an image to the library and save the image URL as a field of a Page type. The function works fine, but when I try to view Page type's data in CMS, it shown a icon instead of the thumbnail. I could still view the full size image by clicking on the icon.

Image Text

I didn't find the thumbnail in folder _thumbnail after executing the function.

Do we have any way to create and get the thumbnail out?

Recent Answers


Virgil Carroll answered on October 8, 2015 15:03

Can you share your code and the API function you are calling...you may have to call a different function to create the thumbnail

0 votesVote for this answer Mark as a Correct answer

Hanh Dang answered on October 9, 2015 04:52 (last edited on October 9, 2015 08:56)

Hi Virgil,

Here is my function to upload and import image to library media. It returns the image url:

private string UploadAndImportImage(Image img, string extension)
{
    string imgUrl = string.Empty;
    if (dataFileUpload.Value.Trim().Length > 0)
    {
        CMS.MediaLibrary.MediaLibraryInfo library = CMS.MediaLibrary.MediaLibraryInfoProvider.GetMediaLibraryInfo("MyMediaLibrary", SiteContext.CurrentSiteName);
        if (library != null)
        {
            string cleanFileName = Guid.NewGuid().ToString();
            string folderDirectory = HttpContext.Current.Server.MapPath(String.Format("~/{0}/media/{1}/Logo/", SiteContext.CurrentSiteName, library.LibraryFolder));
            string mediaFilePath = String.Format("{0}{1}.{2}", folderDirectory, cleanFileName, extension);
            string mediaFileUrl = String.Format("~/{0}/media/{1}/Logo/{2}.{3}", SiteContext.CurrentSiteName, library.LibraryFolder, cleanFileName, extension);
            if (!File.Exists(mediaFilePath))
            {
                #region Create File in Media Library Directory
                //Check if directory exists
                if (!Directory.Exists(folderDirectory))
                    Directory.CreateDirectory(folderDirectory);
                //Save image to path
                img.Save(mediaFilePath);
                #endregion
                // Create new media file object
                CMS.MediaLibrary.MediaFileInfo mediaFile = new CMS.MediaLibrary.MediaFileInfo();
                if (mediaFile != null)
                {
                    // Create file info
                    FileInfo file = FileInfo.New(mediaFilePath);
                    if (file != null)
                    {
                        // Set the properties
                        mediaFile.FileName = cleanFileName;
                        mediaFile.FileTitle = "File title"
                        mediaFile.FileDescription = "File description";
                        mediaFile.FilePath = string.Format("Logo/{0}.{1}", cleanFileName, extension);
                        mediaFile.FileExtension = file.Extension;
                        mediaFile.FileMimeType = string.Format("image/{0}", extension);
                        mediaFile.FileSiteID = SiteContext.CurrentSiteID;
                        mediaFile.FileLibraryID = library.LibraryID;
                        mediaFile.FileSize = file.Length;

                        // Create the media file
                        CMS.MediaLibrary.MediaFileInfoProvider.SetMediaFileInfo(mediaFile);
                        LogoImage.ImageUrl = imgUrl = mediaFileUrl;
                    }
                    else
                    {
                        imgUrl = string.Empty;
                    }
                }
                else
                {
                    imgUrl = string.Empty;
                }
            }
            {
                imgUrl = string.Empty;
            }
        }
        {
            imgUrl = string.Empty;
        }
    }
    return imgUrl ;
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.