Umbraco GetMedia-Complete example
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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步