2014-10-02 78 views
11

我正在嘗試使用本文中提出的方法AngularJS: Display blob (.pdf) in an angular app顯示一個二進制文件。這在Chrome和FF中很好地工作,但IE 11給我「錯誤:訪問被拒絕」。 有沒有人知道它是否與Blob對象有關,並可以指向正確的方向? 這裏是我的js代碼:在IE中顯示二進制文件(pdf)11

$http.get(baseUrl + apiUrl, { responseType: 'arraybuffer' }) 
      .success(function (response) {     
      var file = new Blob([response], { type: 'application/pdf' }); 
      var fileURL = URL.createObjectURL(file); 
      $scope.pdfContent = $sce.trustAsResourceUrl(fileURL); 
      }) 
      .error(function() {       
      }); 

和我的html:

<div ng-controller="PDFController" class="modal fade" id="pdfModal" tabindex="-1" role="dialog" aria-hidden="true"> 
<div class="modal-dialog modal-lg"> 
    <div class="modal-content" onloadstart=""> 
     <object data="{{pdfContent}}" type="application/pdf" style="width:100%; height:1000px" /> 
    </div> 
</div> 

+0

解決方法:我終於使用了[FileSaver.js](https://github.com/eligrey/FileSaver.js/),但如果任何人都可以告訴我,我仍然會很感激爲什麼上述技術在IE 11中不起作用... – jymuk 2014-10-06 07:24:04

+1

我也遇到過這個問題,你是否曾經在沒有FileSaver.js的情況下工作? – Madhatter5501 2014-10-29 20:43:31

+0

不,我沒有遺憾... – jymuk 2014-11-03 09:25:12

回答

5

IE 11的blob塊顯示屏,您需要使用以下命令:

    var byteArray = new Uint8Array(someByteArray); 
       var blob = new Blob([byteArray], { type: 'application/pdf' }); 
       if (window.navigator && window.navigator.msSaveOrOpenBlob) { 
        window.navigator.msSaveOrOpenBlob(blob); 
       } 
       else { 
        var objectUrl = URL.createObjectURL(blob); 
        window.open(objectUrl); 
       }