在IE 11中显示二进制文件(pdf)

时间:2014-10-02 12:55:33

标签: javascript angularjs pdf blob internet-explorer-11

我正在尝试使用此帖AngularJS: Display blob (.pdf) in an angular app中建议的方法显示二进制文件。这在Chrome和FF中运行良好,但是IE 11给了我"错误:访问被拒绝"。 有谁知道它是否与Blob对象有关并且可以指向正确的方向? 这是我的js代码:

$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>

1 个答案:

答案 0 :(得分:7)

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);
                }