我唯一能想到的就是使用那些好的 ol' ActiveX 对象:
var axFile = new ActiveXObject("Scripting.FileSystemObject");
var fileObj = axFile.getFile(document.getElementById('<%= fupAttachment.ClientID %>').value);
var fileSize = {bytes: fileObj.size,
kBytes: Math.round(fileObj.size/1024),
mBytes: Math.round((fileObj.size/1024)/1024)};
这应该为旧版本的 IE 提供支持,完整版本可能类似于:
var axFile, fileSize,
fuDocument = document.getElementById('<%= fupAttachment.ClientID %>');
if (fuDocument.files)
{
fileSize = fuDocument.files[0].size || 0;//default value = 0
}
else
{
axFile = new ActiveXObject("Scripting.FileSystemObject");
fileSize = (axFile.getFile(fuDocument.value) || {size:0}).size;//default to object literal, with size: 0 property --> avoids errors, and defaults to size value of zero
}
return fileSize;//console.log, alert... whatever you want