Copied to clipboard

Flag this post as spam?

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


  • Michael Chart 15 posts 35 karma points
    Jun 20, 2013 @ 18:42
    Michael Chart
    0

    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

  • Michael Chart 15 posts 35 karma points
    Jun 21, 2013 @ 13:51
    Michael Chart
    0

    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

  • Mark Slade 48 posts 109 karma points
    Apr 07, 2014 @ 10:34
    Mark Slade
    0

    Hi Michael,

    Did you ever get an answer to this? I'm facing the same issue.

    Mark

  • Michael Chart 15 posts 35 karma points
    Apr 08, 2014 @ 10:03
    Michael Chart
    0

    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

  • Gerty Engrie 130 posts 490 karma points c-trib
    Jun 20, 2014 @ 11:25
    Gerty Engrie
    1

    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 

Please Sign in or register to post replies

Write your reply to:

Draft