Copied to clipboard

Flag this post as spam?

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


  • Saied 349 posts 674 karma points
    Oct 02, 2015 @ 04:19
    Saied
    0

    Access image from media section at runtime?

    Hello,

    I have the following structure in my media section:

    • Ford

      • Mustang
        • mustang.png
    • Cadillac

      • Escalade
        • escalade.png

    I have a model that holds the model and make of a vehicle. When I render a page, I pass it this model. I want to to be able to show the image from my media section that corresponds to the model data passed in, so if I passed in Escalade, I want to grab the image from the Escalade folder.

    I have seen examples of getting the image by id, but I want to know if there is a way I can grab images from folders by the name.

    Thanks

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Oct 02, 2015 @ 06:12
    Dave Woestenborghs
    0

    I think the easiest way is to add a mediapicker to your page and let the editors select a picture. This way you don't need to query the entire media library for a matching image.

    Dave

  • Saied 349 posts 674 karma points
    Oct 02, 2015 @ 12:06
    Saied
    0

    Dave,

    Can you clarify? So if I have a root folder called product images, I can add this folder to a product page and on that page, grab the image I want?

    How would I do this with razor syntax if I had something like:

    • Product Images
      • Ford
        • Mustang
          • 2015
            • image

    Should I then add the Product Images to my Product page after I added a Media Picker property on the Product document type?

    Let's say I wanted to grab the image from the nested folder in razor if my folder alias was productImages?

    Thanks for the help, Saied

  • Claushingebjerg 939 posts 2574 karma points
    Oct 02, 2015 @ 12:59
    Claushingebjerg
    1

    If you add a media picker datatype to your document type, and make sure you check "Pick multiple items"

    enter image description here

    You can then pick multiple images.

    You should then be able to show the picked images like this

    @if (Model.Content.HasValue("Pictures")){
    var imagesList = Model.Content.GetPropertyValue<string>("Pictures").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
    var imagesCollection = Umbraco.TypedMedia(imagesList).Where(x => x != null);
    
    foreach (var imageItem in imagesCollection)
        {      
            <img src="@imageItem.Url" />      
        }}
    

    (code taken from this post)

  • Saied 349 posts 674 karma points
    Oct 02, 2015 @ 15:54
    Saied
    0

    How would I modify the code if instead of allowing the user to pick multiple images, they would just pick a folder?

    Then I would just get the images from the folder.

  • Claushingebjerg 939 posts 2574 karma points
    Oct 05, 2015 @ 06:27
Please Sign in or register to post replies

Write your reply to:

Draft