Copied to clipboard

Flag this post as spam?

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


  • nic 7 posts 87 karma points
    Oct 13, 2013 @ 14:55
    nic
    0

    Get File URL from property

    Hi there

    i seem to be able to retrive a numeric value from a mdia picker property, but what i need is the actual url to the file

    can anyonew help? - here is my code

    if(Model.Content.GetPropertyValue("sampleChapterFile").ToString()!= null)

        {

            sampleChapterFile = Model.Content.GetPropertyValue("sampleChapterFile").ToString();

        }

     

    cheers

     

    Nic

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Oct 13, 2013 @ 15:08
    Jeavon Leopold
    0

    Hi Nic,

    Take a look at the example in the documentation here

    Jeavon

  • nic 7 posts 87 karma points
    Oct 13, 2013 @ 16:55
    nic
    0

    Hi Jeavon

    Thanks for that

    How can i set the url value to a variable in my code and then use it in the page below?

    The value will not always exist

    cheers

    Nic

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Oct 13, 2013 @ 21:09
    Jeavon Leopold
    0

    Hi Nic,

    How about

    @{
        var mediaItemUrl = string.Empty;
        if(Model.Content.HasValue("sampleChapterFile")){
            mediaItemUrl = Umbraco.TypedMedia(Model.Content.GetPropertyValue("sampleChapterFile")).GetPropertyValue("umbracoFile");  
        }   
    }
    Further down 
    @{
      if (!string.IsNullOrEmpty(mediaItemUrl){
        Do something with @mediaItemUrl
     }
    }
    

    Jeavon

  • nic 7 posts 87 karma points
    Oct 13, 2013 @ 21:47
    nic
    0

    thanks for that, what if the i need to pull the value back in a loop, like the below:

     

    @foreach(var article in Model.Content.Children().Where(c => c.DocumentTypeAlias =="Items" && c.IsVisible()).Take(maxNumArticlesToShow))

    {

        HtmlString details = article.GetPropertyValue<HtmlString>("details");

    HtmlString thumb = article.GetPropertyValue<HtmlString>("thumbnail");

    var img = string.Empty;

    if (article.HasValue("thumbnail")){                                         

            var dynamicMediaItem = Umbraco.Media(Model.Content.GetPropertyValue("thumbnail"));

    img = dynamicMediaItem.umbracoFile;

        }

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Oct 13, 2013 @ 22:52
    Jeavon Leopold
    0

    Hi Nic,

    That looks fine, the img variable is local to the foreach loop, so I'm not quite sure what your are after?

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft