Copied to clipboard

Flag this post as spam?

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


  • Charles Afford 1163 posts 1709 karma points
    Apr 15, 2013 @ 13:38
    Charles Afford
    0

    Creating a media object with the v6.0 API? HELP!!

    I am trying to create a new media object with the V6 API.  Has anyone got any ideas using Umbraco.Core,Models namespace?

    Thanks.  Charlie :)

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Apr 15, 2013 @ 15:16
    Morten Christensen
    0

    Hi Charles,

    In v6 you would do something like this:

    var mediaService = ApplicationContext.Current.Services.MediaService; 
    //CreateMedia takes the name of the media you are created, the id of the parent and finally the alias of the MediaType that you want the media to be based on.
    var media = mediaService.CreateMedia("myMediaFile", -1, "Image");
    mediaService.Save(media);

     And if you want to upload an image that is posted as a HttpPostedFile you simply add it to the umbracoFile property:

    media.SetValue("umbracoFile", thePostedFile);
    //And then save it similar to the above snippet
    mediaService.Save(media);
    

    Hope this helps.

     

    - Morten

  • Charles Afford 1163 posts 1709 karma points
    Apr 15, 2013 @ 15:30
    Charles Afford
    0

    Thanks so much i will give that a try :)

  • Charles Afford 1163 posts 1709 karma points
    Apr 15, 2013 @ 15:58
    Charles Afford
    0

    Hi, when i say creating a media object i may have got my self slightly wrong :).. sorry.

    In V4.10 i was able to create a media object by doing:

    new Media(ID);

    So do i now have to instance a service, create my media in order to create a media object?

    Thanks for your help :)

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Apr 15, 2013 @ 16:53
    Morten Christensen
    0

    Okay, so you want to get an existing media object and not create a new one.

    You still use the MediaService, but use the GetById method:

    var media = mediaService.GetById(1234);

     

    - Morten

Please Sign in or register to post replies

Write your reply to:

Draft