2015-12-29 115 views
0

以下是我的ASP.NET MVC的一部分:当用户试图在IE 9浏览器中上传文件时,它在此行上抛出错误this.files [0]。尺寸; 因为IE 9浏览器确实supoort HTML 5 API(的FileReader)在上传IE9浏览器之前用JavaScript检查文件大小浏览器

<input type="file" name="FileToUpload" id="FileToUpload" style="width: 100%;" /> 
 

 
<!-- begin snippet: js hide: false -->

$('#FileToUpload').bind('change', function() { 
 
      
 
       var sizeInMB = ''; 
 
       
 
        /* Other */ 
 
         this.files[0].size gets the size of your file. 
 
        var sizeInBytes = this.files[0].size; 
 
         var sizeInMB = (sizeInBytes/(1024 * 1024)).toFixed(2); 2000 
 
        sizeInMB = $([sizeInBytes]).map(function() { return (this/1024/1024).toFixed(2) })[0]; 
 
       
 
      
 
       
 
     });

所以我碰巧使用这个片段在i执行此线 $(“#FileToUpload”)[0] .value我不'得到文件路径我得到这个这样的 (C:\ fakepath \ MORETHAN2gbticket.zip)

是否有其他方式可以获取文件大小适用于IE 9浏览器。一种方法是使用Flash。那么我需要使用插件like(uploadify上传jQuery ) 有没有其他我可以不使用插件获取文件大小 任何人都可以请指导我。

var file = $("#FileToUpload"); 
      var objFSO = new ActiveXObject("Scripting.FileSystemObject"); 
      var filePath = $("#FileToUpload")[0].value; 
      var objFile = objFSO.getFile(filePath); 
      var fileSize = objFile.size; //size in kb 

回答

0

没有! IE 9版没有的FileReader API,但原型名为文件阿比实验室

enter image description here

你可以在这里@MDN检查兼容性表。


一个建议是使用回退从服务器获取fileSize。

,如:

if(IE > 9){ 
    // use FileApi 
}else{ 
    // put a call to the server to get the 
    // file size. an ajax call would be fine. 
} 
+0

如果我做一个Ajax调用,文件超过2 GB不ü认为它会抛出一个错误。因为在asp.net中,我们可以上传呸高达2 GB的我想要禁止用户上传2GB – Luke