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!
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)
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:
I'm calling this from 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
Hi Lewis Nice to talk to you again.
Why are you getting the id of the ContentType?
Also, have you tried get the media item by path and then get the id from it?
Cheers
Paul
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
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 thenmediaImage.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)
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
is working on a reply...