Format (humanize) file byte size presentation in javascript


/ Published in: JavaScript
Save to your folder(s)

I use this in addition to the
number_format (http://snipplr.com/view/5945/javascript-numberformat--ported-from-php/)
snippit in SWFUpload to display the size of files a user is preparing to upload.


Copy this code and paste it in your HTML
  1. function size_format (filesize) {
  2. if (filesize >= 1073741824) {
  3. filesize = number_format(filesize / 1073741824, 2, '.', '') + ' Gb';
  4. } else {
  5. if (filesize >= 1048576) {
  6. filesize = number_format(filesize / 1048576, 2, '.', '') + ' Mb';
  7. } else {
  8. if (filesize >= 1024) {
  9. filesize = number_format(filesize / 1024, 0) + ' Kb';
  10. } else {
  11. filesize = number_format(filesize, 0) + ' bytes';
  12. };
  13. };
  14. };
  15. return filesize;
  16. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.