2015-12-29 187 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