Umbraco GetMediaComplete example CQ

Supported by Nova Outsourcing

Many Umbraco developers will run into the situation to get all the attributes returned by the GetMedia method. Here we go with a complete example to get all attributes returned by GetMedia.

Attributes Table

umbracoFile return the virtual path of the file which can be accessed from the url.
umbracoWidth return the image width if the file is an image
umbracoHeight return the height if the file is an image
umbracoBytes return the file size
umbracoExtension return the file extension

Sample code

The sample code below assume that you have a Media Picker type field named thumbnail. It will show the image on the page. If the image is not existing, the space of the image will not be rendered. The code below focuses on GetMedia and using attributes returned by GetMedia.

<xsl:variable name="mediaId" select="thumbnail"/>
            <xsl:variable name="media" select="umbraco.library:GetMedia($mediaId, 0)" />
            <xsl:if test="$media">
              <img src="{$media/umbracoFile}" width="{$media/umbracoWidth}" height="{$media/umbracoHeight}" align="left" />
            </xsl:if>

Note: The code above only applies to Umbraco 4.7 or later. I haven’t done experiment on 4.5 or earlier versions.

I will be glad to answer any questions on the issue.

 

Supported by Nova Outsourcing

原文地址:https://www.cnblogs.com/czy/p/2590471.html