Copied to clipboard

Flag this post as spam?

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


  • Henrik Sunesen 84 posts 281 karma points
    Aug 30, 2016 @ 12:20
    Henrik Sunesen
    0

    MediaService.Save to return the id of the image?

    Hello,

    On website each member has a profile page , the member has the option to upload an "profile image".

    It works fine with the upload, but when I try to set the image id in the mediapicker on the member it fails ("just leaves it blank").

    I have tried so many things, so I hope someone can help.

    [HttpPost]
        public ActionResult UploadMemberImage(MemberImageUpload model)
        {
            if (!ModelState.IsValid)
            {
                return CurrentUmbracoPage();
            }
            else
            {
                //create new media images
                var media = Services.MediaService.CreateMedia(model.File.FileName, 1347, "Image");
                media.SetValue("umbracoFile", model.File);
                media.SetValue("customCreatorId", model.memberid);
    
                Services.MediaService.Save(media);
    
                setMemberImage(model.memberid);
    
                return RedirectToCurrentUmbracoUrl();
            }     
        }
    
    public void setMemberImage(int id)
            {
                var member = Services.MemberService.GetById(id);
                var currectFolder = Umbraco.TypedMedia(1347).Children.Where(x => x.GetPropertyValue("customCreatorId").ToString() == member.Id.ToString());
    
                if (currectFolder.Count() > 0)
                {
                    if (currectFolder.Count() > 1)
                    {
                        var imageToDelete = currectFolder.Where(x => x.GetPropertyValue<int>("customCreatorId") == id).First();
                        var deleteMedia = Services.MediaService.GetById(imageToDelete.Id);
    
                        Services.MediaService.Delete(deleteMedia);
                        Services.MediaService.EmptyRecycleBin();
                    }
                    try
                    {
                        member.SetValue("image", currectFolder.First().Id);
                    }
                    catch (Exception)
                    {
    
                        throw;
                    }
                }
    
                Services.MemberService.Save(member);
            }
    

    I think somehow the image is not registered in the media section when i try to set the profile image.

    So i am wondering: when you call the "Services.MediaService.Save(media);" can it return the id the image was given?

  • Sven Geusens 169 posts 881 karma points c-trib
    Sep 14, 2016 @ 09:54
    Sven Geusens
    100

    Hey Henrik

    The moment you save an IContent or IMedia item, the ID of the item is set if it hadn't been persisted before. (CreateMedia does not persist the item).

    So instead of setMemberImage(model.memberid); do setMemberImage(model.memberid,media);

    Change the signature from public void setMemberImage(int id) to public void setMemberImage(int id,Imedia picture)

    And in the setMemberImage method change member.SetValue("image", currectFolder.First().Id); to member.SetValue("image", media.Id);

  • Henrik Sunesen 84 posts 281 karma points
    Sep 16, 2016 @ 08:59
    Henrik Sunesen
    0

    Thank you, that is a better solution than the one i found out myself ;)

Please Sign in or register to post replies

Write your reply to:

Draft