1、IE瀏覽器下載文件
location = '文件路徑' // 簡單粗暴好用的下載方式(如果是pdf文件以及有pdf插件,會直接預覽,如果不想預覽可以參照下面的方法)
2、文件流下載(IE10, IE11)
先是瀏覽器的判斷
(window.ActiveXObject || "ActiveXObject" in window) true 為IE瀏覽器,false 為非IE瀏覽器
document.documentMode 這個方法很好用,專門判斷IE瀏覽器的版本 返回值類型為數字 8 9 10 11等
var url = res; var xhr = new XMLHttpRequest(); xhr.open('get', url, true); xhr.responseType = "blob"; // 返回類型blob // 定義請求完成的處理函數,請求前也可以增加加載框/禁用下載按鈕邏輯 xhr.onload = function() { // 請求完成 if(this.status === 200) { // 返回200 var blob = this.response; window.navigator.msSaveBlob(blob, '文件.pdf'); // 可自定義文件名 } } // 發送ajax請求 xhr.send();
3、PDF.js(我只找到了最新的版本,只能在IE11上使用,如果有低版本IE都可用的請轉我一份,謝謝)
4、如果業務需要pdf預覽,除了PDF.js外還可以通過判斷用戶是否下載Adobe Reader PDF,直接用這個插件預覽(IE自身是沒有帶pdf預覽插件的,很坑)
// 判斷是否有PDF閱讀器(true為有pdf預覽插件,false表示沒有) 如果有pdf插件,可以直接使用location = '文件路徑' 會自動預覽 function isAcrobatPluginInstall () { //下面代碼都是處理IE瀏覽器的情況 if ((window.ActiveXObject) || (navigator.userAgent.indexOf("Trident") > -1)) { for(x = 2; x < 10; x++) { try { oAcro = eval("new ActiveXObject('PDF.PdfCtrl." + x + "');"); if(oAcro) { return true; } } catch(e) {} } try { oAcro4 = new ActiveXObject('PDF.PdfCtrl.1'); if(oAcro4) return true; } catch(e) {} try { oAcro7 = new ActiveXObject('AcroPDF.PDF.1'); if(oAcro7) return true; } catch(e) {} }else{ //chrome和FF、safrai等其他瀏覽器 return true; } };