Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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?
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);
setMemberImage(model.memberid);
setMemberImage(model.memberid,media);
Change the signature from public void setMemberImage(int id) to public void setMemberImage(int id,Imedia picture)
public void setMemberImage(int id)
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);
member.SetValue("image", currectFolder.First().Id);
member.SetValue("image", media.Id);
Thank you, that is a better solution than the one i found out myself ;)
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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.
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?
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);
dosetMemberImage(model.memberid,media);
Change the signature from
public void setMemberImage(int id)
topublic void setMemberImage(int id,Imedia picture)
And in the setMemberImage method change
member.SetValue("image", currectFolder.First().Id);
tomember.SetValue("image", media.Id);
Thank you, that is a better solution than the one i found out myself ;)
is working on a reply...