Copied to clipboard

Flag this post as spam?

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


  • Andrew Hawken 57 posts 114 karma points c-trib
    Feb 04, 2017 @ 12:00
    Andrew Hawken
    0

    Hi All

    I'm trying to write some code that creates and uploads new media. I can see that the media service allows me to create the media node, but how do I upload the actual data bytes the media node represents. I've seen some sample code writing files directly to the filesystem, but that seems a little clunky, and will presumably fail if the installation is using an Azure filesystem provider or similar.

    Is there an abstraction i'm missing in order to upload the actual bytes representing the media file?

    TIA

  • Dirk De Grave 4534 posts 5980 karma points MVP 3x admin c-trib
    Feb 04, 2017 @ 13:23
    Dirk De Grave
    100

    Hi Andrew,

    It should be enough to use

    var media = _mediaService.CreateMedia(mediaName, mediaFolder.Id, Constants.MediaTypes.Image);
    media.SetValue("umbracoFile", mediaName, stream);
    _mediaService.Save(media);
    

    _mediaService is of IMediaService which you can get from the ApplicationContext.Current.Services

    Of for stream, depends on your implementation, where you get the image from, in our case, it was from db (don't ask...), and we'd be using a MemoryStream to load the image from db.

    Hope this sample helps.

    --Dirk

  • Andrew Hawken 57 posts 114 karma points c-trib
    Feb 04, 2017 @ 14:42
    Andrew Hawken
    0

    That sounds perfect. We're generating Excel spreadsheets on the fly, so turning those into a stream is trivial. I'll give that a try shortly!

  • Andrew Hawken 57 posts 114 karma points c-trib
    Feb 04, 2017 @ 18:06
    Andrew Hawken
    0

    Works perfectly. Many many thanks! H5YR

Please Sign in or register to post replies

Write your reply to:

Draft