2012-12-15 46 views
0

我想要在文件上传控制中的文件大小。 我在Internet Explorer中出现错误。但这是在其他浏览器中工作的代码。 下面的代码使用javascript查找文件上传文件的大小

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

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

+0

使用IE10,IE <= 9不支持文件api – Musa

+0

支持IE的任何其他代码? – Jd30814

+0

@ Jd30814:我提供了一些应该支持IE的代码 - 你有没有机会测试它,如果是的话:它工作吗? –

回答

1

,我能想到的唯一的事情就是用那些好醇” 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 
+0

这一行返回空对象。 'var axFile = new ActiveXObject(“Scripting.FileSystemObject”);'。你能帮忙吗?我的浏览器是FF。 – SMC

+0

@SMC:这将是不可能的,[检查这个问题的答案](http://stackoverflow.com/questions/15086761/alternative-for-the-activex-object-for-the-other-browsers-excluding - 也就是说,/ 15087300#15087300)。 –