Not yet rated

Problem

You have a file size (e.g., from a CFDirectory tag) and you want to output it formatted in the most logical size format.

Solution

Simply call the function, passing only the size in bytes. Designed as a cfc to share w/ all your apps, it can also be easily reformatted to run as a custom tag or inline.

Detailed explanation

<CFCOMPONENT DISPLAYNAME="FileSizeText" 
        HINT="Returns a translated byte/KB/Mb/Gb value">


<CFFUNCTION NAME="getText" ACCESS="public" OUTPUT="false" RETURNTYPE="string"> <!--- <CFINVOKE COMPONENT="FileSizeText" METHOD="getText" RETURNVARIABLE="[RETURNVARIABLE]"> <CFINVOKEARGUMENT NAME="size" VALUE="[int]"> </CFINVOKE> Returns: [RETURNVARIABLE] = string --->
<CFARGUMENT NAME="size" DEFAULT="0" REQUIRED="yes"> <CFIF (NOT IsNumeric(ARGUMENTS.size)) OR (ARGUMENTS.size LTE 0)> <CFSET outText = "Size Unknown"> <CFELSEIF ARGUMENTS.size LT 1024> <CFSET outText = "#ARGUMENTS.size# bytes"> <CFELSEIF ARGUMENTS.size LT 1048576> <CFSET outText = "#NumberFormat(ARGUMENTS.size/1024, "_")# Kb"> <CFELSEIF ARGUMENTS.size LT 1073741824> <CFSET outText = "#DecimalFormat(ARGUMENTS.size/1048576)# Mb"> <CFELSE> <CFSET outText = "#DecimalFormat(ARGUMENTS.size/1073741824)# Gb"> </CFIF> <CFRETURN outText> </CFFUNCTION>
</CFCOMPONENT>

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes