Copied to clipboard

Flag this post as spam?

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


  • aroyce 7 posts 27 karma points
    May 29, 2012 @ 23:14
    aroyce
    0

    Image path from umbraco.library.GetMedia is wrong

    Hi, I am new to Umbraco so I might be doing something wrong here but seems fairly straight forward.

    I have a user control with a repeater on it. It displays text and an image. When each item binds it calls a function to get the image for the ID.

    <td valign="top" style='width: 235px; height: 235px  padding: 10px; background-image: url(<%# getImage(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "Recipe Image"))) %>);'>

     

    The function getImage() returns this value: /media/16056/ice_cream_cookie_sandwich.jpg20121540112jpg

    public string getImage(int ImageID)
    {
    var media = umbraco.library.GetMedia(ImageID, false);
    if (media != null && media.Current != null)
    {
    media.MoveNext();
    return media.Current.Value;
    }
    return "";
    }

     

     

    Any ideas why the filename has the extra numbers appended to it? I could substring these but have to believe there is a better way. Thanks

    Andrew

  • bob baty-barr 1180 posts 1294 karma points MVP
    May 30, 2012 @ 00:29
    bob baty-barr
    0

    in your var statement for media... add /umbracoFile -- see if that get's you the right path.

  • aroyce 7 posts 27 karma points
    May 31, 2012 @ 16:07
    aroyce
    0

    Thanks, that helped me along the right path. I ended up search for both your tip and the getmedia and found this

    http://forum.umbraco.org/yaf_postst4929_Getmedia-in-usercontrol.aspx

     

    Here is what I am now using:

     

    public string getImage(int ImageID)
            {
                XPathNodeIterator xn = umbraco.library.GetMedia(ImageID, false);
                xn.MoveNext();

                XPathNodeIterator xn2 = xn.Current.Select("data[@alias='umbracoFile']");
                xn2.MoveNext();
                return xn2.Current.Value;
            }

     

  • Rafael Dominguez 2 posts 22 karma points
    May 29, 2013 @ 00:29
    Rafael Dominguez
    0

    Here's an update for the latest schema

     var xpNodeIter = umbraco.library.GetMedia(ImageID,false);
     xpNodeIter.MoveNext();
     var xpNodeIter2 = xpNodeIter.Current.Select("umbracoFile"); 
     xpNodeIter2.MoveNext();
     var imageUrl = xpNodeIter2.Current.Value;  

Please Sign in or register to post replies

Write your reply to:

Draft