2017-07-17 35 views
1

我想呈現一個PDF內部iframe。它在Mozilla(v54)和Chrome(v59)上運行良好,但是當我單擊加載PDF的鏈接時,IE(v11)中沒有任何反應。經過多次調試,我發現Chrome/Firefox中的URL是blob:http://localhost:37444/5a8e7fed-cd61-4c58-904c-fad2ae169718,而在IE(v11)中它是blob:B7395CB5-169D-471F-BB8F-AA90EAFB6DDB。爲什麼URL.createObjectURL(BLOB)不附加在IE(V11)的HTTP請求URL.createObjectURL(斑點)不在IE中創建正確的URL 11

function (iframe, url, headers) {    

     var xhr = new XMLHttpRequest(); 

     xhr.open('GET', url); 
     xhr.onreadystatechange = handler; 
     xhr.responseType = "arraybuffer"; 

     headers.forEach(function (header) { 
      xhr.setRequestHeader(header[0], header[1]); 
     }); 
     xhr.send(); 

     function handler() { 
      if (this.readyState === this.DONE) { 

       if (this.status === 200) { 
        var blob = new Blob([xhr.response], { type: "application/pdf" }); 
        var objectUrl = URL.createObjectURL(blob);      

        iframe.src = objectUrl; 
       } else { 
        console.error('XHR failed', this); 
       } 
      } 
     } 
+3

[在IE 11中顯示二進制文件(pdf)]的可能副本(https://stackoverflow.com/questions/26161314/displaying-binary-file-pdf-in-ie-11) – demo

+0

您建議的解決方案說你必須在IE(v11)中打開或保存,但我想在模態框中呈現PDF。如果有的話,請提出解決方法 –

回答

0

IE不創建的,因爲安全原因,這些斑點對象的URL我think.So使用VAR的ObjectURL = URL.createObjectURL (blob);不會給你可以在iframe或embed標籤內使用的源url。 我面臨同樣的問題,並搜索了很多關於修復。但不能得到答案。而是我解決它如下。 您可以使用下面的IE

if (bowser.msie && window.navigator.msSaveOrOpenBlob) { 
    navigator.msSaveOrOpenBlob(file, fileName); 
}else{ 
    //do what you were doing for other than IE 
} 

上述IE的代碼將提示用戶是否希望保存該文件,或直接將其打開。 用戶可以點擊按鈕'打開',然後IE將顯示PDF,而不會將其下載到默認閱讀器中。