获取文件大小

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;
}

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