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");
iMedia.SetValue("umbracoFile", HttpPostedFileBase);
_mediaService.Save(iMedia);
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?
media.SetValue("umbracoFile", ???);
_mediaService.Save(media);
using (Stream stream = new MemoryStream(documentContent))
{
// Since umbracoFile is empty, there aint no path to take.
_mediaService.SetMediaFileContent("NoFilePath", stream);
}
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
IMediaService _mediaService;
IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
// The new way to do it.
IMedia media = _mediaService.CreateMediaWithIdentity("Filename", 1234, "File");
media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", "FilenameWithExtension", Stream);
_mediaService.Save(media);
Byte[]
byte[] byteArray;
using (Stream stream = new MemoryStream(byteArray))
{
media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", "FilenameWithExtension", stream);
}
How to create media file from a byte array
Hi all.
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.
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.
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:
https://github.com/umbraco/Umbraco-CMS/issues/5102
Hi Josip.
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.
Byte[]
HttpPostedFileBase
Hi Bo,
that is great. Thanks for sharing your solution.
is working on a reply...