使用javascript在文件上载中查找文件大小

使用javascript在文件上载中查找文件大小,javascript,asp.net,Javascript,Asp.net,我想在文件上传控制中调整文件大小。 我在Internet Explorer中遇到错误。但这是在其他浏览器中工作的代码。 以下代码 var fuDocument = document.getElementById('<%= fupAttachment.ClientID %>'); var file = fuDocument.files[0]; if (file != null) { var fileSize = file.si

我想在文件上传控制中调整文件大小。 我在Internet Explorer中遇到错误。但这是在其他浏览器中工作的代码。 以下代码

var fuDocument = document.getElementById('<%= fupAttachment.ClientID %>');
  var file = fuDocument.files[0];
            if (file != null) {
                var fileSize = file.size;
}
var fuDocument=document.getElementById(“”);
var file=fuDocument.files[0];
如果(文件!=null){
var fileSize=file.size;
}

错误“files.0”为空或不是对象

我唯一能想到的是使用那些好的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)};
var-axFile=new-ActiveXObject(“Scripting.FileSystemObject”);
var fileObj=axFile.getFile(document.getElementById(“”).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
var axFile,文件大小,
fuDocument=document.getElementById(“”);
if(fuDocument.files)
{
fileSize=fuDocument.files[0].size | | 0;//默认值=0
}
其他的
{
axFile=newActiveXObject(“Scripting.FileSystemObject”);
fileSize=(axFile.getFile(fuDocument.value)| |{size:0}).size;//默认为对象文字,size:0属性-->避免错误,默认为大小值为零
}
返回文件大小//console.log,警报。。。随便你

使用IE10,IE10还有其他支持IE的代码吗?@Jd30814:我提供了一些应该支持IE的代码-您是否有机会对其进行测试,如果有:它是否工作?这行返回null对象
var-axFile=new-ActiveXObject(“Scripting.FileSystemObject”)。你能帮忙吗。我的浏览器是FF。@SMC:那是不可能的。