Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Bo Jacobsen 470 posts 1917 karma points
    Apr 25, 2019 @ 14:13
    Bo Jacobsen

    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.

    // Umbraco 7
    var _mediaService = ApplicationContext.Current.Services.MediaService;
    var iMedia = _mediaService.CreateMediaWithIdentity("Filename", 1234, "File");
    iMedia.SetValue("umbracoFile", HttpPostedFileBase);

    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", ???);
    using (Stream stream = new MemoryStream(documentContent))
        // Since umbracoFile is empty, there aint no path to take.
        _mediaService.SetMediaFileContent("NoFilePath", stream);

    Does anyone know what to do?

  • Josip 189 posts 654 karma points c-trib
    Apr 25, 2019 @ 19:19

    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:

  • Bo Jacobsen 470 posts 1917 karma points
    Apr 26, 2019 @ 06:42
    Bo Jacobsen

    Hi Josip.

    Thanks for answering. You saved me alot of searhing.

    I will come back when i find the way to do it.

  • Bo Jacobsen 470 posts 1917 karma points
    Apr 26, 2019 @ 07:16
    Bo Jacobsen

    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);


    byte[] byteArray;
    using (Stream stream = new MemoryStream(byteArray))
        media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", "FilenameWithExtension", stream);


    HttpPostedFileBase httpPostedFileBase;
    media.SetValue(_contentTypeBaseServiceProvider, "umbracoFile", httpPostedFileBase.FileName, httpPostedFileBase.InputStream);
  • Josip 189 posts 654 karma points c-trib
    Apr 26, 2019 @ 11:37

    Hi Bo,

    that is great. Thanks for sharing your solution.

Please Sign in or register to post replies

Write your reply to: