I am trying to create a media item programatically, including uploading an image in Umbraco 6. I am using the new MediaService and calling mediaService.CreateMedia(mediaName, parentFolder, "Image"). That is all working fine, but what I don't know how to do is how to upload the file.
Which seems to get the media root directory and create the folders and files manually. This doesn't seem to work in Umbraco 6, as HttpContext.GetGlobalResourceObject("AppSettings", "MediaFilePath") is null.
I suppose I was expecting there to be some methods exposed in the API for doing this sort of thing. For now, I have just used the code from the above post (http://our.umbraco.org/forum/developers/api-questions/3477-Creating-Media-Programatically) and just replaced HttpContext.GetGlobalResourceObject("AppSettings", "MediaFilePath") with a static reference to the media folder.
Umbraco 6 - uploading media files programatically
I am trying to create a media item programatically, including uploading an image in Umbraco 6. I am using the new MediaService and calling mediaService.CreateMedia(mediaName, parentFolder, "Image"). That is all working fine, but what I don't know how to do is how to upload the file.
I have seen this post:
http://our.umbraco.org/forum/developers/api-questions/3477-Creating-Media-Programatically
Which seems to get the media root directory and create the folders and files manually. This doesn't seem to work in Umbraco 6, as HttpContext.GetGlobalResourceObject("AppSettings", "MediaFilePath") is null.
How should I upload media files in Umbraco 6?
Thanks
I suppose I was expecting there to be some methods exposed in the API for doing this sort of thing. For now, I have just used the code from the above post (http://our.umbraco.org/forum/developers/api-questions/3477-Creating-Media-Programatically) and just replaced HttpContext.GetGlobalResourceObject("AppSettings", "MediaFilePath") with a static reference to the media folder.
Michael
Hi Michael,
Did you ever get an answer to this? I'm facing the same issue.
Mark
Hi Mark,
No - I ended up just doing what I described above. Not really sure what the official way to do such a thing is - sorry I can't be much more help.
Michael
Hello :-)
have you tried this? Mediaurl is a path here, so something like /myimages/test.jpg
var mediaService = ApplicationContext.Current.Services.MediaService;
var media = mediaService.CreateMedia(property + "-" + id, 2111, "Image");
media.SetValue("umbracoFile", mediaurl.Substring(mediaurl.LastIndexOf("/") + 1), new System.IO.FileStream(System.Web.Hosting.HostingEnvironment.MapPath(mediaurl), System.IO.FileMode.Open));
mediaService.Save(media);
return media.Id;
Gerty
is working on a reply...