How do I upload a file to my Sitecore library in code using the ASP FileUpload control?

Here is what I tried and does not seem to work. I am not getting any errors, but it does not seem to add the file to the media library.

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 problem is that I really don't understand what some of the different parameters and properties do. What is the Destination Property for MediaCreatorOptions? Is this just a folder supposed to be? It is assumed that it also has an element name? What are the three parameters to the CreateFromStream method? The first one seems to be a stream - I understand. But the second said "FileName". What should it be? If I create from a stream, why do I need to tell Sitecore file_name?

Any help would be appreciated.

+5
2

, , API Sitecore. . Sitecore, ... . , .

API SDN:

- API-

Sitecore.Resources.Media.MediaCreator Sitecore.Resources.Media.MediaCreatorOptions . , /Sitecore/Media Library/Images/Sample 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)
+4

, , , , . @divamatrix , , - ( ) .

"" MediaCreatorOptions?

@divamatrix, Destination MediaCreatorOptions - , , MediaItem Sitecore Media (, sitecore/Media Library/Images/Created Image)

, ? , ?

, , . , , , (. ).

CreateFromStream? , , - . "FileName". ? , Sitecore _?

, Destination MediaCreatorOptions, GetItemPath options.Destination, . Destination, GetItemPath . , CreateFromStream ( ) Destination MediaCreatorOptions filePath , , , GetItemPath , , .

+2

All Articles