javascript根据文件字节数返回文件大小
1 function getFileSize(fileByte) { 2 var fileSizeByte = fileByte; 3 var fileSizeMsg = ""; 4 if(fileSizeByte < 1024){ 5 fileSizeMsg = fileSizeByte + "B"; 6 }else if (fileSizeByte < 1048576) { 7 fileSizeMsg = window.parseFloat(((fileSizeByte / 1024)).toFixed(2)) + "KB"; 8 }else if (fileSizeByte == 1048576) { 9 fileSizeMsg = "1MB"; 10 }else if (fileSizeByte > 1048576 && fileSizeByte < 1073741824){ 11 fileSizeMsg = window.parseFloat(((fileSizeByte / (1024 * 1024))).toFixed(2)) + "MB"; 12 }else if (fileSizeByte > 1048576 && fileSizeByte == 1073741824){ 13 fileSizeMsg = "1GB"; 14 }else if (fileSizeByte > 1073741824 && fileSizeByte < 1099511627776){ 15 fileSizeMsg = window.parseFloat(((fileSizeByte / (1024 * 1024 * 1024))).toFixed(2)) + "GB"; 16 }else{ 17 fileSizeMsg = "文件超过1TB"; 18 } 19 return fileSizeMsg; 20 }
会根据字节数进行转换成文件大小,如果转换后有小数点则保留两位小数,并且小数点后只有一位或没有小数点时去除最后无用的0.