Copied to clipboard

Flag this post as spam?

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


  • David 10 posts 30 karma points
    Jun 23, 2015 @ 14:57
    David
    0

    CreateMedia File Contents

    Hi,

    I'm trying to use the Umbraco API to store an image.

    I followed various posts like this to save the media contents from a Stream or a Byte Array. The problem is the overload seems to be different:

    var f = mediaService.CreateMedia(fileName, mediaRootId, mediaType);
    // Assumes the image._Image is a Stream - you may have to do some extra work here...
    
    f.SetValue(Constants.Conventions.Media.File, fileName, (Stream)image._Image); // Real magic happens here.
    

    I guess this must be an earlier version of the API because the only overload I have for SetValue takes 2 strings!

    My Code:

    using (MemoryStream ms = new MemoryStream())
    {
        using (FileStream fs = File.OpenRead(tempFilepath))
        {
            fs.CopyTo(ms);
        }
    
        mediaItem.SetValue("umbracoFile", ... what to do here????);
    
        Services.MediaService.Save(mediaItem);
    }
    

    How do I fix that so the file contents get set?

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Jun 23, 2015 @ 16:11
    Kevin Jump
    1

    Hi,

    I think you need to include Umbraco.Core.Models in your using statements to get the extended SetValues showing up

    using Umbraco.Core.Models;
    

    once you've done that, you can set name and filestream.

    item.SetValue("umbracoFile", Path.GetFileName(file), s);
    

    but have a look at the code for uSync.ContentEdition. ( https://github.com/KevinJump/jumoo.usync/blob/master/jumoo.usync.content/helpers/FileHelper.cs#L258 )

    because this is some trickery around setting values if you are using the Image Cropper (it stores JSON in the umbracoFile value)

  • Ben McKean 272 posts 549 karma points
    Oct 14, 2015 @ 14:37
    Ben McKean
    0

    Thanks, for this was wrestling with this today!

  • David 10 posts 30 karma points
    Jun 23, 2015 @ 16:20
    David
    0

    Fantastic! That worked.

    I haven't looked at uSync yet but I'll keep that in mind about the JSON when I do.

    Thanks for the help.

Please Sign in or register to post replies

Write your reply to:

Draft