You need to sign in to do that
Don't have an account?
Tad Aalgaard 3
How do I display the contentsize from Content object as Kb, Mb, Gb on a Visualforce page?
On the Content object page I can see it dispalyed in Kb or other sizes, but when I display the contentsize field on a Visualforce page it shows the size in bytes. I would like it to appear as Kb, Mb, or Gb. A contractor had used some JavaScript to calcuate the size on another page, but I'd rather not use JavaScript as I'd like to try and stay server side instead of client side. What are my options? Do I use forumula on a second hidden field, is there some Visualforce tag I can use?
public String FileSizeToString(Long Value)
{
/* string representation if a file's size, such as 2 KB, 4.1 MB, etc */
if (Value < 1024)
return string.valueOf(Value) + ' Bytes';
else
if (Value >= 1024 && Value < (1024*1024))
{
//KB
Decimal kb = Decimal.valueOf(Value);
kb = kb.divide(1024,2);
return string.valueOf(kb) + ' KB';
}
else
if (Value >= (1024*1024) && Value < (1024*1024*1024))
{
//MB
Decimal mb = Decimal.valueOf(Value);
mb = mb.divide((1024*1024),2);
return string.valueOf(mb) + ' MB';
}
else
{
//GB
Decimal gb = Decimal.valueOf(Value);
gb = gb.divide((1024*1024*1024),2);
return string.valueOf(gb) + ' GB';
}
}
All Answers
public String FileSizeToString(Long Value)
{
/* string representation if a file's size, such as 2 KB, 4.1 MB, etc */
if (Value < 1024)
return string.valueOf(Value) + ' Bytes';
else
if (Value >= 1024 && Value < (1024*1024))
{
//KB
Decimal kb = Decimal.valueOf(Value);
kb = kb.divide(1024,2);
return string.valueOf(kb) + ' KB';
}
else
if (Value >= (1024*1024) && Value < (1024*1024*1024))
{
//MB
Decimal mb = Decimal.valueOf(Value);
mb = mb.divide((1024*1024),2);
return string.valueOf(mb) + ' MB';
}
else
{
//GB
Decimal gb = Decimal.valueOf(Value);
gb = gb.divide((1024*1024*1024),2);
return string.valueOf(gb) + ' GB';
}
}
For others who may be interested, below is a breakdown of how I accomplished this.
First, I creating a Component Controller called BytesToKbMbGbComponentController.
Then I created a component called BytesToKbMbGbComponent.
Lastly, I put a reference to the BytesToKbMbGbComponent component in my Visualforce page.
Note: the "a" in a.ContentSize is the variable name in my page the refers to the var in my blockTable which is also a Content object.