Copied to clipboard

Flag this post as spam?

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


  • Fredrik 89 posts 108 karma points
    Mar 25, 2011 @ 15:39
    Fredrik
    0

    Finding media url

    How do I get url to my media programatically?

    With xslt Ive succesfully managed with the following:

    <xsl:variable name="mediaId" select="number(./image)" />
          <xsl:if test="$mediaId > 0">     
            <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />     
            <xsl:if test="$mediaNode/umbracoFile">       
              <img src="{$mediaNode/umbracoFile}" alt="[image]" height="170"/>     
            </xsl:if>   
          </xsl:if>

    I want to use C# to achieve this in visual studio!

     return umbraco.library.GetMedia(int.Parse(child3.GetProperty("image").Value), false).ToString();

    this does not work.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 25, 2011 @ 16:45
    Tom Fulton
    0

    Hi Frederik,

    Check out this post, think it will do the trick:  http://our.umbraco.org/forum/developers/api-questions/7993-Get-URL-of-media-file#comment29410

    -Tom

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 25, 2011 @ 16:56
    Jeroen Breuer
    1

    The sample Tom refers to uses the Media api which is pretty slow. I recommend you use library.GetMedia because it uses the umbraco cache system. You can read all the data from the XPathNodeIterator which GetMedia returns. Here is a sample

    //Get the media item from the umbraco cache.
    XPathNodeIterator mediaIterator = library.GetMedia(nodeId, false);
    
    //Convert the XPathNodeIterator to an XDocument.
    XDocument xmlDocument = XDocument.Parse(mediaIterator.Current.OuterXml);
    
    //Get the root element.
    XElement mediaElement = xmlDocument.Root;
    
    string mediaUrl = GetMediaLink(mediaElement)
    
    /// <summary>
    /// Return the media link of an element.
    /// </summary>
    /// <param name="mediaElement"></param>
    /// <returns></returns>
    public static string GetMediaLink(XElement mediaElement)
    {
        XElement filePathElement = DigibizMediaHelper.GetFilePathElement(mediaElement);
    
        if (filePathElement == null)
        {
            return "#";
        }
    
        return filePathElement.Value;
    }
    
    /// <summary>
    /// Get the file path element. If the old xml schema is used we need to fetch this a different way.
    /// </summary>
    /// <param name="mediaElement"></param>
    /// <returns></returns>
    public static XElement GetFilePathElement(XElement mediaElement)
    {
        XElement filePathElement = 
            !UmbracoSettings.UseLegacyXmlSchema
            ?
            mediaElement.Descendants("umbracoFile").FirstOrDefault()
            :
            mediaElement.Descendants("data").Where(x => x.Attribute("alias").Value == "umbracoFile").FirstOrDefault()
            ;
    
        if (filePathElement == null)
        {
            //If the filepath is not found try another option.
            filePathElement =
                !UmbracoSettings.UseLegacyXmlSchema
                ?
                mediaElement.Descendants("fileName").FirstOrDefault()
                :
                mediaElement.Descendants("data").Where(x => x.Attribute("alias").Value == "fileName").FirstOrDefault()
                ;
        }
    
        if (filePathElement == null)
        {
            //If the filepath is still not found try a third option.
            filePathElement =
                !UmbracoSettings.UseLegacyXmlSchema
                ?
                mediaElement.Descendants("file").FirstOrDefault()
                :
                mediaElement.Descendants("data").Where(x => x.Attribute("alias").Value == "file").FirstOrDefault()
                ;
        }
    
        return filePathElement;
    }
    

    This is how I do it in the Digibiz Advanced Media Picker.

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft