Copied to clipboard

Flag this post as spam?

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


  • Daniel 60 posts 174 karma points
    Jun 08, 2016 @ 14:22
    Daniel
    0

    mediaResource.save() for an image

    Hey guys, does anyone have a working sample for the following (the "files" bit to be exact)?

    mediaResourcesave(media, isNew, files)
    

    The official doc only has an example for a folder, but not an Image: http://umbraco.github.io/Belle/#/api/umbraco.resources.mediaResource

  • David Armitage 508 posts 2078 karma points
    Jun 09, 2016 @ 05:13
    David Armitage
    1

    Hi,

    If you are trying to add an image or file I usually create a couple of helper methods to handle this.

    One for an image and one for other types of files. They are slightly different.

    public static IMedia AddMediaImage(int parentId, HttpPostedFile file)
    {
        var ms = ApplicationContext.Current.Services.MediaService;
        var mediaMap = ms.CreateMedia(file.FileName, parentId, Umbraco.Core.Constants.Conventions.MediaTypes.Image);
        mediaMap.SetValue("umbracoFile", file.FileName, file.InputStream);
        ms.Save(mediaMap);
        return mediaMap;
    }
    
    public static IMedia AddMediaFile(int parentId, HttpPostedFile file)
    {
        var ms = ApplicationContext.Current.Services.MediaService;
        var mediaMap = ms.CreateMedia(file.FileName, parentId, Umbraco.Core.Constants.Conventions.MediaTypes.File);
        mediaMap.SetValue("umbracoFile", file.FileName, file.InputStream);
        ms.Save(mediaMap);
        return mediaMap;
    }
    

    Hope this helps

    Kind Regards

    David

  • Daniel 60 posts 174 karma points
    Jun 09, 2016 @ 08:03
    Daniel
    0

    Really appreciate the examples, I kinda had this approach as a backup - I just think it's very odd that we need to write methods (same code every time) for something so generic as saving an image.

    I'd really prefer to use mediaResource.save(media, isNew, files) since it seems designed for the job - but I really can't figure out what goes into files and there seems to be 0 documentation, so I think I'll go with your approach for now!

    Thanks Dave!

  • David Armitage 508 posts 2078 karma points
    Jun 09, 2016 @ 08:58
    David Armitage
    1

    Hopefully someone here has the answer. I would be interested to see if there was a better solution too. Please let me know if you find out anything further.

    Kind Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft