January 6, 2017

Add image to media library programmatically in Sitecore

You can use below technique to add media file to media library in Sitecore.

var options = new Sitecore.Resources.Media.MediaCreatorOptions
   {
       AlternateText =
"",
       FileBased =
false
,
       IncludeExtensionInItemName =
false
,
       KeepExisting =
false
,
       Versioned =
false
,
       Destination =
"/sitecore/media library/Images/Contacts/"
+ ImageName,
       Database = Sitecore.Configuration.
Factory.GetDatabase("master"
)
   };

  
var
filename = Server.MapPath(Img.FileName);
  
var creator = new MediaCreator
();
  
var
mediaItem = creator.CreateFromStream(Img.PostedFile.InputStream, filename, options);

No comments:

Post a Comment