Copied to clipboard

Flag this post as spam?

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


  • Arul Prabakaran 6 posts 90 karma points c-trib
    May 17, 2022 @ 04:44
    Arul Prabakaran
    0

    How to save image as umbraco media programaticaly?

    Hi,

    I'm trying to save a image (profile picture) for registered member but when I send the image (cropped information) as base64 text to the service. Image and folder is getting created, but the content of the image is not getting updated through mediaservice.

    Even the official example to use setValue for stream is not working in this version of Umbraco.

    Current output: enter image description here

    Code used:

    IMember currentMember = _memberService.GetByEmail(userEmail);
    IMedia profilePictureFolder = null;
    IMedia profilePicture = null;
    
    byte[] imageBytes = Convert.FromBase64String(image.ImageData.Substring(image.ImageData.IndexOf(',') + 1));
    
    using (Stream stream = new MemoryStream(imageBytes))
    {
        profilePictureFolder = _mediaService.CreateMedia("Member Profile Pictures", Constants.System.Root, Constants.Conventions.MediaTypes.Folder);
        _mediaService.Save(profilePictureFolder);
    
        profilePicture = _mediaService.CreateMediaWithIdentity($"{currentMember.Name}-{currentMember.Id}", profilePictureFolder.Id, Constants.Conventions.MediaTypes.Image);
        profilePicture.SetValue(Constants.Conventions.Media.File, stream);
        _mediaService.Save(profilePicture);
    
        return Content(profilePicture.Key.ToString());
    }
    

    Thanks in advance.

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    May 17, 2022 @ 06:39
    Paul Seal
    105

    Hi

    I created a GitHub repo to show examples how to do all of this.

    You can register, login, logout, edit profile with image, gallery images, dropdown, checkbox list, radio list properties etc.

    Here is a link to the media upload service I created for it https://github.com/prjseal/Umbraco-9-Membership/blob/master/Umbraco9Membership/Services/MediaUploadService.cs

    Paul

  • Arul Prabakaran 6 posts 90 karma points c-trib
    May 17, 2022 @ 07:09
    Arul Prabakaran
    1

    Thanks Paul, I will implement and verify it.

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    May 17, 2022 @ 16:26
    Paul Seal
    0

    How was that solution for you Arul? Did it work?

  • Arul Prabakaran 6 posts 90 karma points c-trib
    May 18, 2022 @ 13:24
    Arul Prabakaran
    0

    Thanks Paul, this worked as expected.

Please Sign in or register to post replies

Write your reply to:

Draft