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 ;
}