HOME | FERGUSON Digital Blog

UDF File Size Converter

ColdFusion Add comments

Here is a User Defined Function I created that will take a numeric file size value and convert it to abbreviated file size text. The function will append "bytes" if the number is less than 1000. It will strip of the last three numbers if the file size is between 1000 and 999999 and append " kb" to the number. Lastly, it will strip off the last six numbers if the file size is greater than or equal to 1000000 and append " mb" to the number. If you need it to also show gigabyte notation, the last conditional will need to be changed and fourth added to handle the larger file size.

<cffunction name="fnFileSize" access="public" returntype="string">
   <cfargument name="FileSize" type="numeric" required="yes">
   <cfscript>
      //CONVERTS NUMBER INTO FILESIZE WORDS
      ConvertSize=0;
      if (FileSize LT 1000) {
         ConvertSize=FileSize & " bytes";}
      if (FileSize GTE 1000 AND FileSize LTE 999999) {
         ConvertSize=Left(FileSize, Len(FileSize)-3) & " kb";}
      if (FileSize GTE 1000000) {
         ConvertSize=Left(FileSize, Len(FileSize)-6) & " mb"
;}
      return ConvertSize;
   </cfscript>
</cffunction>

If you find this post useful please leave a comment and let me know how you used the information.

0 responses to “UDF File Size Converter”

Leave a Reply




© Copyright 1997-2024, All Rights Reserved Coldfusion and MS SQL2008
Powered by Mango Blog.   Design by FERGUSON Digital
o:Event name="beforeHtmlBodyEnd" />