Copied to clipboard

Flag this post as spam?

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


  • Cookie Monster 7 posts 27 karma points
    Oct 21, 2010 @ 14:22
    Cookie Monster
    0

    Retieve file path of media image

    Hi,

    i have the following code inside a .net usercontrol:

    XPathNodeIterator xn = umbraco.library.GetMedia(NodeID.As<int>(0), false);
    xn.MoveNext();

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

    which should return the file path of the media item (which is a image)

    however what im getting back is the is something like:
    \media\1033\testimage.jpg102476834567jpg
    i have search a couple of topics now with a similar issue and made sure the "data [@alias='umbracoFile']" is correct,  is there something i might be missing, overlooking, maybe a blonde moment ??
    Tnx

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 21, 2010 @ 21:02
    Lee Kelleher
    3

    Hey Cookie Monster,

    Which version of Umbraco are you using? If it's v4.5, then do you know if you are using the legacy XML schema?  It could be that you are using the new XML schema, so the XPath expression isn't returning the correct value.

    To be honest, if you are doing this in .NET code, I wouldn't recommend using the GetMedia method (as XPathNodeIterators are waaaaaay horrid to work with in C#).

    Try something like this:

    int nodeId = 1234; // get this from wherever!
    var media = new umbraco.cms.businesslogic.media.Media(nodeId);
    var file = media.getProperty("umbracoFile");
    string url = file.Value;

    I'd add a couple of checks to make sure that the "media" and "file" variables aren't NULL, etc.

    Cheers, Lee.

  • Zarko 9 posts 32 karma points
    Oct 22, 2010 @ 15:43
    Zarko
    2

    Try something like this:

    url = HttpContext.Current.Server.MapPath(xn2.Current.Value);

Please Sign in or register to post replies

Write your reply to:

Draft