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.
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.
As @divamatrix pointed out, the
Destination
property inMediaCreatorOptions
is where you would like your MediaItem to live in the Sitecore Media Library (ex. /sitecore/Media Library/Images/Created Image)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).
The filename is ignored if you set the
Destination
with theMediaCreatorOptions
because theGetItemPath
method will just returnoptions.Destination
if not null or empty. If you do not setDestination
, theGetItemPath
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 theDestination
inMediaCreatorOptions
, or set thefilePath
to where you would like your item to be and theGetItemPath
method will try to put it there, but may throw exceptions if it is unable to.