How can I upload a file to the Sitecore media library in code using an ASP FileUpload control?

4.6k views Asked by At

Here is what I have tried and it doesn't seem to work. I don't get any errors, but it doesn't seem to add the file to the media library either.

using(new Sitecore.SecurityModel.SecurityDisabler())
{
  if(myFileControl.HasFile)
  {
    MediaCreatorOptions _options = new MediaCreatorOptions();
    _options.Database = Factory.GetDatabase("master");
    _options.FileBased = false;
    _options.IncludeExtensionInItemName = false;
    _options.KeepExisting = false;
    _options.Versioned = false;
    _options.Destination = "/sitecore/media library";
    MediaItem _newFile = MediaManager.Creator.CreateFromStream(myFileControl.FileContent, myFileControl.FileName, _options);
  }
}

My biggest issue is that I don't really understand what some of the different parameters and properties do. What is the "Destination" property for the MediaCreatorOptions? Is it supposed to be just a folder? Is it supposed to have the item name also? What are the three parameters for the CreateFromStream method? The first one seems to be the Stream - I get that. But the second was says "FileName". What is this supposed to be? If I am creating from a Stream why do I need to tell Sitecore the FileName?

Any help would be appreciated.

2

There are 2 answers

0
al3xnull On

Corey, I'm not sure if this is still relevant, but I just ran into the exact same questions you did. @divamatrix did not really answer the stream questions and I would like to fill out this question in case someone else (or myself again) need the answers.

What is the "Destination" property for the MediaCreatorOptions?

As @divamatrix pointed out, the Destination property in MediaCreatorOptions is where you would like your MediaItem to live in the Sitecore Media Library (ex. /sitecore/Media Library/Images/Created Image)

Is it supposed to be just a folder? Is it supposed to have the item name also?

No, it should not be the name of a folder currently in the media library. It should be the path of the item you would like created including the name of the item you would like created (see above).

What are the three parameters for the CreateFromStream method? The first one seems to be the Stream - I get that. But the second was says "FileName". What is this supposed to be? If I am creating from a Stream why do I need to tell Sitecore the FileName?

The filename is ignored if you set the Destination with the MediaCreatorOptions because the GetItemPath method will just return options.Destination if not null or empty. If you do not set Destination, the GetItemPath method will try it's best to give you a proper path in the media library. In essence, when using CreateFromStream (as far as I can tell), set the Destination in MediaCreatorOptions, or set the filePath to where you would like your item to be and the GetItemPath method will try to put it there, but may throw exceptions if it is unable to.

0
divamatrix On

I think that your problem here is that you're not using the proper options for the Sitecore API call. You don't have a real destination.. which is where you would specify the Sitecore item that will become your item... not just a folder. It looks like you're trying to create the media library item.

Per the Content API Book on SDN:

How to Create Media Items Using APIs

You can use the Sitecore.Resources.Media.MediaCreator and Sitecore.Resources.Media.MediaCreatorOptions classes to create media items from files. For example, to create the media item /Sitecore/Media Library/Images/Sample in the Master database from the file C:\temp\sample.jpg:

Sitecore.Resources.Media.MediaCreatorOptions options =  new Sitecore.Resources.Media.MediaCreatorOptions();
options.Database = Sitecore.Configuration.Factory.GetDatabase("master");
options.Language = Sitecore.Globalization.Language.Parse(Sitecore.Configuration.Settings.DefaultLanguage);
options.Versioned = Sitecore.Configuration.Settings.Media.UploadAsVersionableByDefault;
options.Destination = "/sitecore/media library/images/Sample";
options.FileBased = Sitecore.Configuration.Settings.Media.UploadAsFiles;
Sitecore.Resources.Media.MediaCreator creator = new Sitecore.Resources.Media.MediaCreator();
Sitecore.Data.Items.MediaItem sample = creator.CreateFromFile(@"C:\temp\sample.jpg",options)