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 604 posts 2402 karma points
    Apr 25, 2019 @ 14:13
    Bo Jacobsen
    0

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

    Does anyone know what to do?

  • Josip 195 posts 662 karma points c-trib
    Apr 25, 2019 @ 19:19
    Josip
    1

    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

  • Bo Jacobsen 604 posts 2402 karma points
    Apr 26, 2019 @ 06:42
    Bo Jacobsen
    0

    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 604 posts 2402 karma points
    Apr 26, 2019 @ 07:16
    Bo Jacobsen
    102

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

    HttpPostedFileBase

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

    Hi Bo,

    that is great. Thanks for sharing your solution.

Please Sign in or register to post replies

Write your reply to:

Draft