Copied to clipboard

Flag this post as spam?

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


  • Niels Sparenberg 11 posts 72 karma points
    Jan 10, 2014 @ 15:52
    Niels Sparenberg
    0

    Umbraco 6 Media API upload image

    Hi all

    Can anybody tell me how I upload a picture (the actual data) via the media API. I've been searching for a while and can't seem to find anything for the v6 API.

    It looks like in the legacy v4 you can use the mediaServiceSoapClients writeContents (haven't tried it though) but how do you get the raw data of an Image up in the media library?

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 10, 2014 @ 16:32
    Sören Deger
    0

    Hi Niels,

    you can find the documentation here:
    http://our.umbraco.org/documentation/Reference/Management-v6/Models/Media

    I hope this helps.

     

    Best regards

    Sören

  • Niels Sparenberg 11 posts 72 karma points
    Jan 13, 2014 @ 10:23
    Niels Sparenberg
    0

    Cheers for the link Søren, but I have looked the documentation through and can't figure out from it where the raw data of the image goes.

    Let say I make a method that downloads that umb logo from the top:

    using (WebClient client = new WebClient())
    {
        Stream s = client.OpenRead("http://our.umbraco.org/css/img/top_logo.png");
        using (MemoryStream ms = new MemoryStream())
        {
            s.CopyTo(ms);
        }
    }
    

    The ms.ToArray() now has the raw data of the img as a byte[] but where do you put that on the media element?

    Or do you have to create the file via IO?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 13, 2014 @ 11:33
    Jeroen Breuer
    0
  • Niels Sparenberg 11 posts 72 karma points
    Jan 24, 2014 @ 14:54
    Niels Sparenberg
    0

    Hi Jeroen

    Thanks for the reply. It seems that the link you have posted is using a HttpPostedFileBase. This i guess comes from an upload control and in my case I don't work with an upload control.

    What i have is the raw physical data of a file. How would you programmatically upload images from your hard drive to umbraco via mediaservice?

  • Niels Sparenberg 11 posts 72 karma points
    Feb 25, 2014 @ 09:57
    Niels Sparenberg
    100

    Haven't replied but here is how i solved the problem, but it is a bit hacky imo think it uses something I found in another forum posts that i can't find now:

    I used this to wrap up the image data as byte[]

    public class HttpPostedFileWrapper : HttpPostedFileBase
    {
        private Stream stream;
        private string contentType;
        private string fileName;
    
        public HttpPostedFileWrapper(string fileName, string contentType, byte[] imgData)
        {
            this.fileName = fileName;
            this.stream = new MemoryStream(imgData);
            this.contentType = contentType;
    
        }
    
        public override int ContentLength
        {
            get { return (int)stream.Length; }
        }
    
        public override string ContentType
        {
            get { return contentType; }
        }
    
        public override string FileName
        {
            get { return fileName; }
        }
    
        public override Stream InputStream
        {
            get { return stream; }
        }
    
        public override void SaveAs(string filename)
        {
            using (var file = File.Open(filename, FileMode.CreateNew))
                stream.CopyTo(file);
        }
    }
    

    I then created an UmbracoApiController to get access to MediaService with the following method:

       [HttpPost]
        public int CreateImage(Image image)
        {
            var newImage = mediaService.CreateMedia(image.Name, image.ParentID, image.MediaType);
            HttpPostedFileWrapper httpPostedWrapper = new HttpPostedFileWrapper(image.ImageName, image.ContentType, image.ImageData);
            newImage.SetValue("umbracoFile", httpPostedWrapper);
    
            mediaService.Save(newImage);
            return newImage.Id;
        }
    

    The Image obj contains the data needed to create the image (ContentType, Name, Data etc.)

    For ~4mb + images u need to fiddle with your web.config.

Please Sign in or register to post replies

Write your reply to:

Draft