Howto display the filesize next to a download link

feddersen
Community Manager
Community Manager
3 2 1,012

It's a common pratice in UI design to display the filesize next to a download link. That can be accomplished very easily within FirstSpirit.

Within a linktemplate, you can access the filesize like this:

$CMS_VALUE(ref(#link.sitestoreref).size)$

This will display the filesize in bytes, which isn't very userfriendly. However, we provide a convenience function for a human readable output:

$CMS_VALUE(ref(#link.sitestoreref).size.humanReadable("**","0.0 {U}"))$

This construct will output the filesize in the highest unit possible.

If you want to stick to a particular unit, kilobytes for example, you could do:

$CMS_VALUE(ref(#link.sitestoreref).size.humanReadable("KiB","0.0 {U}"))$

Possible units are:

KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB (base 2)

kB, MB, GB, TB, PB, EB, ZB, YB (base 10)

** will display the highest base 2 unit possible

* will display the highest base 10 unit possible

2 Comments
hoebbel
Crownpeak employee
Crownpeak employee

Of course this will work only with media-store elements, not with sitestore pagerefs!

[Just a reminder, since within the example  "ref(#link.sitestoreref)" is used Smiley Wink

sglock
I'm new here

Hi,

here's my two cents from a usability point of view:

I recommend displaying downloads below 1 MB without fractional digits since the exact size seems rather irrelevant in most cases. What's more important for the user is the type of document that's going to be downloaded. You may also want to use non-breaking spaces to prevent "orphans" in your layout.

The following example will print out e.g. "Manual [pdf, 1,2 MB]" or "Poster [jpg, 723 kB]".

$CMS_IF(ref(#link.mediaref).size > 999999)$
     $CMS_SET(downloadsize, ref(#link.mediaref).size.humanReadable("*","#0.0 {U}"))$
$CMS_ELSE$
     $CMS_SET(downloadsize, ref(#link.mediaref).size.humanReadable("*","#0 {U}"))$
$CMS_END_IF$

 
$CMS_VALUE(#link.text + "\&nbsp[" + ref(#link.mediaref).extension + ",\&nbsp" + downloadsize.replaceAll(" ","\&nbsp") + "]")$

(Sorry for the backslash before the "&nbsp" - it's only there to make the non-breaking space visible in this comment-section. (Semicolons are missing, too.) There's probably a better way to do that.)

Version history
Last update:
‎05-27-2010 10:06 AM
Updated by: