码迷,mamicode.com
首页 > 其他好文 > 详细

获取文件大小

时间:2018-02-02 18:36:51      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:大小   document   class   amp   ret   var   byte   fun   public   

Js:

function getFileSize()
{
var size=0;
var files = document.getElementById("file").files;
for(var i=0; i< files.length; i++){
var fileSize=files[i].size;
//var count=Math.round(fileSize/1024.00);
//alert(s);
size+=fileSize;
}
alert(formatBytes(size));
}

function formatBytes(bytes) {
if(bytes < 1024) return bytes + " Bytes";
else if(bytes < 1048576) return(bytes / 1024).toFixed(2) + " KB";
else if(bytes < 1073741824) return(bytes / 1048576).toFixed(2) + " MB";
else return(bytes / 1073741824).toFixed(3) + " GB";
};

 

C#:

public static string GetFileSize(string sFullName)
{
long lSize = 0;
if (System.IO.File.Exists(sFullName))
lSize = new FileInfo(sFullName).Length;
return CountSize(lSize);
}

public static string CountSize(long Size)
{
string m_strSize = "";
long FactSize = 0;
FactSize = Size;
if (FactSize < 1024.00)
m_strSize = FactSize.ToString("F2") + " Byte";
else if (FactSize >= 1024.00 && FactSize < 1048576)
m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
else if (FactSize >= 1048576 && FactSize < 1073741824)
m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
else if (FactSize >= 1073741824)
m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
return m_strSize;
}

获取文件大小

标签:大小   document   class   amp   ret   var   byte   fun   public   

原文地址:https://www.cnblogs.com/Tang-X/p/8406169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!