Copied to clipboard

Flag this post as spam?

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


  • Craig100 1091 posts 2380 karma points c-trib
    Nov 13, 2012 @ 21:52
    Craig100
    0

    Get Image by Name

    Hi,

    Can't work out how to display an image I have in a Media subfolder in razor. It's not attached to a page at all and I want to be able to call it by it's name parameter.

    dynamic mediaItem = @Model.Media.backgrounds.Image.Name("Biography Page").umbracoFile;

    Doesn't work of course. 

    The image is in the Media section in a directory called "backgrounds", it can be any file name but the "Name" parameter must be "Biography Page". I'm probably going at it from the wrong angle but can't find out enough about the Umbraco razor object model to navigate it myself. 

    Any help would be appreciated.

    Craig

  • P.A. Leleulya 1 post 71 karma points
    Oct 05, 2020 @ 10:30
    P.A. Leleulya
    0

    Umbraco version 8.6.4

    // get image /media/myFolder/myFile.jpg

    var rootFoldername = "myFolder" var fileName = "myFile";

    var imageByName = Umbraco.Web.Composing.Current.UmbracoHelper? .MediaAtRoot()? .FirstOrDefault(f => f.Name.Equals(rootFoldername))? .Children()? .FirstOrDefault(d => d.Name.Equals(fileName));

Please Sign in or register to post replies

Write your reply to:

Draft