2015-09-29 118 views
3
"change .btn-file :file"  : "getfileinfo", 

getfileinfo: function(e){ 

    var fileInput = document.getElementById('fileupload'); 
    var file = fileInput.files[0]; 

    if(!file || (file.type != 'text/plain' && file.type != 'text/csv' && file.type != 'application/csv' && file.type != 'application/vnd.ms-excel' && file.type != 'text/csv')){ 
     $('.alert-file').show(); 
     $('.alert-file').delay(4000).fadeOut('slow'); 
     return false; 
    } 
    var reader = new FileReader(); 

    reader.onload = function(e) { 
     $('#fileData').val(reader.result); 
     $('#fileName').val(file.name); 

    }; 
    reader.readAsDataURL(file); 

} 

file.type如果我在IE11和IE Edge中使用,則爲null。所有其他瀏覽器工作正常。IE 11和IE Edge獲取file.type失敗

有人可以幫助我嗎?

+0

請格式化你的代碼。 –

+0

在詢問之前也做一些頭腦風暴。你有沒有看過http://stackoverflow.com/questions/27980428/javascript-file-type-is-not-working-on-ie-11 –

+0

是的,但我看到,但沒有答案 – Gopi

回答

0

測試用例

<input type="file" onchange="document.body.innerText = this.files[0].type" />

回報MIME爲SVG,PNG,ZIP,但對於PDF

的Windows 8.1專業版64位下重現了IE11

Version: 11.0.9600.18053 
Update Versions: 11.0.24 (KB3093983) 
返回 ""

B ug已經報告並且以「無法複製」https://connect.microsoft.com/IE/Feedback/Details/2009205關閉。它提到爲什麼IE團隊無法重現:

請記住,IE只能確定MIME類型基於其擴展名的文件時,安裝在該MIME類型(如ADOBE READER)的處理程序應用程序系統並在註冊表的HKEY_CLASSES_ROOT註冊表配置單元內創建映射。如果註冊表中不存在映射,則IE無法知道該文件的MIME類型。

其中20 GB新安裝的Windows沒有空間放file工具

+0

這是一個怎樣的答案? – Bonatti

+0

@Bonatti這是不可能重現,我已經清理了測試用例(源代碼清晰度和文件類型),描述確切的環境,應該幫助 – sergeykish

+0

在stackoverflow中的答案應該是一種方法爲OP來解決他/她的問題,或尋找文件來了解問題的方法....你的回答既不是,也不是一個新來者檢查你的「答案」,他仍然不明白該怎麼做... [請閱讀此鏈接]( http://stackoverflow.com/help/how-to-answer) – Bonatti

0

我能夠重現上免費的Windows 7/IE 11 VM(available here),其嚴重不來這個錯誤與默認的PDF處理程序,所以不知道什麼是mimetype。

結果,我想出了唯一的解決方法是這樣的(對於PDF文件的例子 - 改變應用程序/ PDF格式和PDF需要的話):

if(file.type == 'application/pdf' || file.name.toLowerCase().endsWith('pdf')) 
{ 
    //code to execute 
} 

請注意,這不會不工作在IE瀏覽器的所有版本的endsWith的填充工具 - 所以這裏是你需要的任何地方在您的代碼(來自Mozilla page here)的填充工具片斷

if (!String.prototype.endsWith) { 
    String.prototype.endsWith = function(searchString, position) { 
     var subjectString = this.toString(); 
     if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) { 
     position = subjectString.length; 
     } 
     position -= searchString.length; 
     var lastIndex = subjectString.lastIndexOf(searchString, position); 
     return lastIndex !== -1 && lastIndex === position; 
    }; 
}