Copied to clipboard

Flag this post as spam?

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


  • Lewis Smith 208 posts 617 karma points c-trib
    Apr 19, 2018 @ 15:01
    Lewis Smith
    1

    Upload image and get its Id programmatically

    Hi all,

    I have the following code that shoud upload an image to umbraco, then get its id which i then use to link to content!

    Image upload:

    public int UploadImageToMedia(string MediaItemTitle, int ParentId, string MediaTypeAlias, string ImageUrl)
            {
                var mediaId = 0;
                var mediaService = Services.MediaService;
                var request = WebRequest.Create(ImageUrl);
                var webResponse = request.GetResponse();
                var responseStream = webResponse.GetResponseStream();
                if(responseStream != null)
                {
                    var originalImage = new Bitmap(responseStream);
                    Guid g;
                    g = Guid.NewGuid();
                    var path = Server.MapPath("/Media-Auto/" + MediaItemTitle + g + ".jpg");
    
                    originalImage.Save(path, ImageFormat.Jpeg);
    
                    FileStream fileStream = new FileStream(path, FileMode.Open);
                    var fileName = fileStream.Name;
    
                    var mediaImage = mediaService.CreateMedia(MediaItemTitle, ParentId, "Image");
                    mediaImage.SetValue("umbracoFile", fileName, fileStream);
                    mediaId = mediaImage.ContentType.Id;
                    mediaService.Save(mediaImage);
                    responseStream.Dispose();
                    webResponse.Dispose();
                    originalImage.Dispose();
                }
                return mediaId;
            }
    

    I'm calling this from here:

    var Id = UploadImageToMedia("title" , 1293" , "Image", "image url goes here")
    

    The issue im getting is the Id that is returned is not the correct Id. AKA the Id is different to what i find when finding the image in the media area!

    Could anyone shed some light on my issues!?

    Thanks. Lewis

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Apr 19, 2018 @ 15:13
    Paul Seal
    1

    Hi Lewis Nice to talk to you again.

    Why are you getting the id of the ContentType?

    mediaId = mediaImage.ContentType.Id;
    

    Also, have you tried get the media item by path and then get the id from it?

    IMedia media = mediaService.GetMediaByPath(mediaFilePath);
    

    Cheers

    Paul

  • Lewis Smith 208 posts 617 karma points c-trib
    Apr 19, 2018 @ 15:37
    Lewis Smith
    0

    Hi Paul,

    Good to speak to you too! Hope to see you at codegarden this year!

    That's a mistake in the code, yesterday the code was returning the incorrect Id when it was both mediaImage.ContentType.Id and `mediaImage.

    I will give GetMediaByPath a go!

    Thanks, Lewis

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Apr 19, 2018 @ 15:58
    Kevin Jump
    0

    Hi

    the media id mediaImage.Id should be valid once the item is saved (but not before).

    but if you create with mediaService.CreateMediaWithIdentity instead of create the media item will be created in the first call and then mediaImage.Id will be valid

    (but only do that if you are sure there is no way for your code to stop the creation after the first call)

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Apr 19, 2018 @ 16:01
    Paul Seal
    0

    Hi Lewis Yeah, I'll be at CodeGarden this year. Looking forward to it.

    Are you on the Umbraco Slack Group? http://umbracians.chat/ If so, DM me.

    Cheers

    Paul

Please Sign in or register to post replies

Write your reply to:

Draft