Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I am trying to create a media file from a received byte array. But i cannot get it to work.
In Umbraco 7 we could just put the HttpPostedFileBase in the umbracoFile field.
// Umbraco 7
var _mediaService = ApplicationContext.Current.Services.MediaService;
var iMedia = _mediaService.CreateMediaWithIdentity("Filename", 1234, "File");
Now this does not work in Umbraco 8. I then found a method on the MediaService called SetMediaFileContent that accept a filepath and a stream. But i dont have a filepath since the umbracoFile property is empty.
// Umbraco 8
IMediaService _mediaService; // Just to show the interface, since it is now DI.
byte documentContent; // Just to show the byte array.
var media = _mediaService.CreateMediaWithIdentity("Filename", 1234, "File");
// I dunno if the umbracoFile should be set with something?
using (Stream stream = new MemoryStream(documentContent))
// Since umbracoFile is empty, there aint no path to take.
Does anyone know what to do?
Hi Bo , i have the same problem like you do, plus I am begginer in c#.
Check this out, I think its related to our problem:
Thanks for answering. You saved me alot of searhing.
I will come back when i find the way to do it.
Okay, so there is a little more work to do in order to get it work.
Now you need the IContentTypeBaseServiceProvider and a Stream.
// Umbraco 8
using Umbraco.Core; <- Important..
// Needed Interfaces
// The new way to do it.
IMedia media = _mediaService.CreateMediaWithIdentity("Filename", 1234, "File");
media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", "FilenameWithExtension", Stream);
using (Stream stream = new MemoryStream(byteArray))
media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", "FilenameWithExtension", stream);
media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", httpPostedFileBase.FileName, httpPostedFileBase.InputStream);
that is great. Thanks for sharing your solution.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted