Cecil Theodore
Cecil Theodore

Reputation: 9939

Get file size from media folder in Umbraco using XSLT

Below is a part of my Umbraco xslt where I am outputting a PDF file link and the size of the file. The problem I am having, is targetting the size attribute of the media file. The size attribute has the alias of umbracoBytes.

I just seem to not be able to target this.

So all I am outputting at the moment is the link to open the PDF, but not the file size.

Anyone who can help would be greatly appreciated. Thanks.

<td>
      <xsl:if test="document= ''">
        <xsl:value-of select="@nodeName"/>
      </xsl:if>
      <xsl:if test="document != ''">
      <a target="_blank">   
        <xsl:attribute name="href">
          <xsl:value-of select="umbraco.library:GetMedia(document, 'false')/umbracoFile"/>
        </xsl:attribute>
        <xsl:value-of select="@nodeName"/>       
        <xsl:variable name="size" select="data [@alias = 'umbracoBytes']" />
          <xsl:variable name="sizeAndSuffix">
              <xsl:choose>
                      <xsl:when test="$size &gt;= 1073741824">
                              <xsl:value-of select="format-number($size div 1073741824,'#,###')"/>
                              <xsl:text>GB</xsl:text>
                      </xsl:when>
                      <xsl:when test="$size &gt;= 1048576">
                              <xsl:value-of select="format-number($size div 1048576,'#,###')"/>
                              <xsl:text>MB</xsl:text>
                      </xsl:when>
                      <xsl:when test="$size &gt;= 1024">
                              <xsl:value-of select="format-number($size div 1024,'#,###')"/>
                              <xsl:text>KB</xsl:text>
                      </xsl:when>
                      <xsl:when test="$size &gt; 0 and $size &lt; 1024">
                              <xsl:value-of select="format-number($size div 0,'#,###')"/>
                              <xsl:text>Bytes</xsl:text>
                      </xsl:when>
                      <xsl:otherwise>
                              <xsl:text>0 Bytes</xsl:text>
                      </xsl:otherwise>
              </xsl:choose>
        </xsl:variable>

      </a>
      </xsl:if>
    </td>

Upvotes: 2

Views: 1283

Answers (2)

Cecil Theodore
Cecil Theodore

Reputation: 9939

I managed to work it out.

I removed the wrapping variable and created a variable named size which selects the alias UmbracoBytes (the file size)

I then pass the variable through the choose function and this outputs the correct size extension.

Thanks for all your suggestions I really appreciate that!

   <xsl:variable name="size" select="umbraco.library:GetMedia(document, 'false')/umbracoBytes"/>

          <xsl:choose>
                  <xsl:when test="$size &gt;= 1073741824">
                          <xsl:value-of select="format-number($size div 1073741824,'#,###')"/>
                          <xsl:text>GB</xsl:text>
                  </xsl:when>
                  <xsl:when test="$size &gt;= 1048576">
                          <xsl:value-of select="format-number($size div 1048576,'#,###')"/>
                          <xsl:text>MB</xsl:text>
                  </xsl:when>
                  <xsl:when test="$size &gt;= 1024">
                          <xsl:value-of select="format-number($size div 1024,'#,###')"/>
                          <xsl:text>KB</xsl:text>
                  </xsl:when>
                  <xsl:when test="$size &gt; 0 and $size &lt; 1024">
                          <xsl:value-of select="format-number($size div 0,'#,###')"/>
                          <xsl:text>Bytes</xsl:text>
                  </xsl:when>
                  <xsl:otherwise>
                          <xsl:text>0 Bytes</xsl:text>
                  </xsl:otherwise>
          </xsl:choose>

Upvotes: 1

Goran Mottram
Goran Mottram

Reputation: 6304

The problem (I think) is that because you didn't prepend your $size variable select attribute with any target, so by default it uses $currentPage or the current value in a loop iteration.

Try assigning the GetMedia statement to a variable and then get the data from that. Your code appears to have syntax used in different versions of Umbraco, so I can't quite tell which one you're using. Different versions of Umbraco use different underlying XML structure.

If using < Umbraco 4.5.1

<xsl:variable name="myDocument" select="umbraco.library:GetMedia(document, 'false')" />
<a target="_blank">        
    <xsl:attribute name="href">
      <xsl:value-of select="$myDocument/data[@alias='umbracoFile']"/>
    </xsl:attribute>
    ...
    <xsl:variable name="size" select="$myDocument/data [@alias = 'umbracoBytes']" />
    ...
</a>

If using >= Umbraco 4.5.1

<xsl:variable name="myDocument" select="umbraco.library:GetMedia(document, 'false')" />
<a target="_blank">        
    <xsl:attribute name="href">
      <xsl:value-of select="$myDocument/umbracoFile"/>
    </xsl:attribute>
    ...
    <xsl:variable name="size" select="$myDocument/umbracoBytes" />
    ...
</a>

Upvotes: 1

Related Questions