-
6 Feb 2011 2:35 PM #1
Ext.utl.Format.fileSize() -- doesn't support GB and TB file sizes
Ext.utl.Format.fileSize() -- doesn't support GB and TB file sizes
fileSize() function will only calculate up to the megabyte range. This should be updated to handle much larger file sizes that will exist on a typical web-server.
Original Source for EXTjs 3.3.1
[http://dev.sencha.com/deploy/dev/doc...rmat-fileSize]
Below is my recommended patch for 3.x and 4.xPHP Code:
/**
* Simple format for a file size (xxx bytes, xxx KB, xxx MB)
* @param {Number/String} size The numeric value to format
* @return {String} The formatted file size
*/
fileSize : function(size) {
if (size < 1024) {
return size + " bytes";
} else if (size < 1048576) {
return (Math.round(((size*10) / 1024))/10) + " KB";
} else {
return (Math.round(((size*10) / 1048576))/10) + " MB";
}
},
PHP Code:
/**
* Simple format for a file size (xxx bytes, xxx KB, xxx MB and upto xxx PB)
* @param {Number/String} size The numeric value to format
* @return {String} The formatted file size
*/
fileSize : function(size) {
if (size < 1024) { //pow(2,10)
return size + " bytes";
} else if (size < 1048576) { //pow(2,20)
return (Math.round(((size*10) / 1024))/10) + " KB";
} else if (size < 1073741824) { //pow(2,30)
return (Math.round(((size*10) / 1048576))/10) + " MB";
} else if (size < 1099511627776) { //pow(2,40)
return (Math.round(((size*100) / 1073741824))/100) + " GB";
} else if (size < 1125899906842624) { //pow(2,50)
return (Math.round(((size*100) / 1099511627776))/100) + " TB";
} else { //pow(2,60)
return (Math.round(((size*100) / 1125899906842624))/100) + " PB";
}
},
Perfection as a goal is a nice idea that can point one in a specific direction. However, since "perfection" is an ever changing (evolving?) and moving target, one must admit that perfection can never be obtained...
When in doubt, check the d4mn source code!
-
6 Feb 2011 2:52 PM #2
Related post
Related post
I found this thread after I created my post. However, it's in the premium section...
http://www.sencha.com/forum/showthre...o-calculate-GBPerfection as a goal is a nice idea that can point one in a specific direction. However, since "perfection" is an ever changing (evolving?) and moving target, one must admit that perfection can never be obtained...
When in doubt, check the d4mn source code!
-
10 Feb 2011 6:48 AM #3
//bump++;
Perfection as a goal is a nice idea that can point one in a specific direction. However, since "perfection" is an ever changing (evolving?) and moving target, one must admit that perfection can never be obtained...
When in doubt, check the d4mn source code!
-
2 Mar 2011 2:08 PM #4
Adding Thresholds and decimal places
Adding Thresholds and decimal places
I considered the necessity to add thresholds to the filesize funtion. By default, the function below will return a string that is similar to the Windows style of filesizes. However, some cases exists where you want to keep the threshold at 100%, thereby only indicating that file by actual size...
Additionally, I've added another optional parameter for decimal places. This makes it flexible to display more accurate filesizes upto 4 decimal places.
For Example:
Ext.util.format.fileSize('934123');
//returns: "912.23 KB"
Ext.util.format.fileSize('973265', 1,2);
//returns: "950.45 KB"
Ext.util.format.fileSize('973265', 1,1);
//returns: "950.5 KB"
Ext.util.format.fileSize('957523265', 0.85,3);
//returns: "0.892 GB"
Ext.util.format.fileSize('957523265', .98);
//returns: "913.17 MB"
PHP Code:
/**
* Simple format for a file size (xxx.x bytes, xxx.xx KB, xxx.xx MB and upto xxx PB)
* @param {Number/String} size The numeric value to format
* @param {Number/String} threshold (optional) The numeric value to force size to use the next larger filesize indicator
* @param {Number/String} decimal (optional) The numeric value of decimal places to show in the returned string
* @return {String} The formatted file size
*/
fileSize : function(size, threshold, decimal) {
threshold= threshold? threshold: 0.95;//set default to a Windows style of FileSizes
threshold= Math.min(Math.max(threshold, 0.75), 1); //should be between 75-100%
decimal= decimal? decimal: 2;//set default to 2 decimal units
decimal= Math.min(Math.max(decimal, 0), 4); //should be between 0-4
if (size < 1024*threshold) { //pow(2,10)
return size + " bytes";
} else if (size < 1048576*threshold) { //pow(2,20)
return (Math.round(((size*Math.pow(10,decimal)) / 1024))/Math.pow(10,decimal)) + " KB";
} else if (size < 1073741824*threshold) { //pow(2,30)
return (Math.round(((size*Math.pow(10,decimal)) / 1048576))/Math.pow(10,decimal)) + " MB";
} else if (size < 1099511627776*threshold) { //pow(2,40)
return (Math.round(((size*Math.pow(10,decimal)) / 1073741824))/Math.pow(10,decimal)) + " GB";
} else if (size < 1125899906842624*threshold) { //pow(2,50)
return (Math.round(((size*Math.pow(10,decimal)) / 1099511627776))/Math.pow(10,decimal)) + " TB";
} else { //pow(2,60)
return (Math.round(((size*Math.pow(10,decimal)) / 1125899906842624))/Math.pow(10,decimal)) + " PB";
}
},
Perfection as a goal is a nice idea that can point one in a specific direction. However, since "perfection" is an ever changing (evolving?) and moving target, one must admit that perfection can never be obtained...
When in doubt, check the d4mn source code!
-
11 Jan 2013 1:16 AM #5
Another function very simple
Another function very simple
For our french friends !!!
PHP Code:formatSize: function(bytes){
var TabUnite = Array("octets", "Ko", "Mo", "Go", "To", "Po", "Eo", "Zo", "Yo");
var i = 0 ;
while(bytes >= 1024 && i < TabUnite.length-1){
bytes = bytes / 1024;
i++;
}
bytes = Math.round(bytes*100)/100;
return bytes+" "+TabUnite[i];
}
Thank you for reporting this bug. We will make it our priority to review this report.
Similar Threads
-
Ext.util.Format.fileSize is not localized
By mhankus in forum Ext 3.x: BugsReplies: 0Last Post: 22 Jan 2011, 11:21 AM -
ExtJS 2.1 file sizes?
By RyanZec in forum Ext 2.x: Help & DiscussionReplies: 2Last Post: 21 Apr 2008, 4:29 AM -
Date modification: support for 'P' format
By orgutus in forum Community DiscussionReplies: 3Last Post: 4 Jan 2008, 8:30 AM -
File Sizes??
By ReyBango in forum Community DiscussionReplies: 2Last Post: 14 Jun 2007, 4:29 AM



Reply With Quote