function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tad Aalgaard 3Tad 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? 

Best Answer chosen by Tad Aalgaard 3
Saikishore Reddy AengareddySaikishore Reddy Aengareddy
/* UTIL Method */
        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

Saikishore Reddy AengareddySaikishore Reddy Aengareddy
/* UTIL Method */
        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';
            }    
        }
This was selected as the best answer
Tad Aalgaard 3Tad Aalgaard 3
Saikishore, thanks for the code.  I took your code and applied it to my situation.  

For others who may be interested, below is a breakdown of how I accomplished this.

First, I creating a Component Controller called BytesToKbMbGbComponentController.
public class BytesToKbMbGbComponentController {
   
    /*
     * Converts a String value that represents the bytes in a file to text such as 2 KB, 4.1 MB, etc
     * @author Tad Aalgaard
     * @Date 2/20/2015
  */
   
    public String controllerValue;
   
    public void setControllerValue (String s) {
        controllerValue = s.toUpperCase();
        Long longValue = Long.valueOf(s);
       
        if (longValue < 1024)
            controllerValue =  string.valueOf(longValue) + ' Bytes';
        else
            if (longValue >= 1024 && longValue < (1024*1024))
        {
            //KB
            Decimal kb = Decimal.valueOf(longValue);
            kb = kb.divide(1024,2);
            controllerValue = string.valueOf(kb) + ' KB';
        }
        else
            if (longValue >= (1024*1024) && longValue < (1024*1024*1024))
        {
            //MB
            Decimal mb = Decimal.valueOf(longValue);
            mb = mb.divide((1024*1024),2);
            controllerValue = string.valueOf(mb) + ' MB';
        }
        else
        {
            //GB
            Decimal gb = Decimal.valueOf(longValue);
            gb = gb.divide((1024*1024*1024),2);
           
            controllerValue = string.valueOf(gb) + ' GB';
        }   
       
    }
   
    public String getControllerValue() {
        return controllerValue;
    }
}

Then I created a component called BytesToKbMbGbComponent.
 
<apex:component controller="BytesToKbMbGbComponentController">
  <apex:attribute name="componentValue" description="Number to convert to Kb Mb Gb sizes" type="String" required="required" assignTo="{!controllerValue}"/>
{!controllerValue}   
</apex:component>

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.
 
<c:BytesToKbMbGbComponent componentValue="{!a.ContentSize}"/>
Tina Y.T. ChangTina Y.T. Chang
@Saikishore Reddy Aengareddy, @Tad Aalgaard 3 - I found your question when working on a similar issue here. Your answers have helped me a lot. Really appreciate it! : )