Save MediaData blob when uploading files in EPiServer CMS 7.5

Here is just a little example of how you can save the uploaded file in to the MediaData files system in EPiServer 7.5.

The object FileSaveItem is a local object to store some useful data about the file

Publikation is a pagetype that holds the assetfolder reference



Code:
FileSaveItem item = new FileSaveItem { Success = false };

HttpPostedFile file = Request.Files[0];

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var blobFactory = ServiceLocator.Current.GetInstance<BlobFactory>();

//Get a new empty file data
var file1 = contentRepository.GetDefault<GenericMedia>(publikation.AssetFolder);


string filename = DateTime.Now.ToString("yyyy-MM-dd_HHmm") + Path.GetExtension(file.FileName);

item.Filename = filename;
file1.Name = filename;
file1.Description = filename;

var blob = blobFactory.CreateBlob(file1.BinaryDataContainer, Path.GetExtension(file.FileName));

item.FileSizeBytes = file.InputStream.Length;

blob.Write(file.InputStream);

file1.BinaryData = blob;
item.FileRef = contentRepository.Save(file1, SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

Inga kommentarer:

Skicka en kommentar