从 angular7 应用程序下载 PDF 时,如何解决 IE 浏览器中的错误 "access is denied"?

How can i resolve the error "access is denied" in IE browsers when downloading a PDF from my angular7 application?

在我的 angular 7 应用程序中,我有一个 API,调用时会下载 PDF。

这是我获取 PDF 的方法:

getPdf() {
  const payload = { applicantId: this.idHeader, codes: 
  this.codeHeader + ':0', genderType: this.gender, data: this.data }

  this.service.getPdfConfirmationView(payload).subscribe((pdfResponse: 
  any) => {
    let dataType = 'application/pdf';
    let binaryData = [];
    binaryData.push(pdfResponse);
    let downloadLink = document.createElement('a');
    downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: dataType }));
    if (pdfResponse)
      downloadLink.setAttribute('download', 'ConfirmationReport');
    document.body.appendChild(downloadLink);
    downloadLink.click();
    })
  }

这是我连接到 API

的服务
  getPdfConfirmationView(payload) {
    return this.http.get(environment.apiUrl + 
    '/v1/report/getPdfConfirmationView',
    { headers: this.getSearchApiHeaders(payload), responseType: 'blob' });
  }

这在 Chrome 浏览器中运行良好,我可以单击 link 并将 PDF 文件下载到我的计算机。但是,在 Internet Explorer 11 中,我收到此错误消息:

ERROR Error: Access is denied.
  "ERROR"
    [object Error]{description: "Access is d...", message: "Access is d...", name: "Error", number: -2147024891, stack: "Error: Acce..."}
    [functions]
    __proto__[object Error] {...}
    description"Access is denied. ...
    message"Access is denied. ...
    name"Error"
    number-2147024891
    stack"Error: Access is denied. ...

如何在 IE 浏览器中解决此问题?

IE 不支持您在获取 PDF 的方法中使用的 URL.createObjectURL()。 IE 有自己的 API 用于创建和下载文件,称为 msSaveBlobmsSaveOrOpenBlob

msSaveBlobmsSaveOrOpenBlob方法的区别在于前者只向用户提供保存按钮,而后者同时提供保存和一个打开按钮。您可以在 IE 中像这样使用它们:

window.navigator.msSaveOrOpenBlob(blobData, fileName);  

更多信息,您可以参考this article in Microsoft and this thread

-------------------------------------------- - - - 编辑 - - - - - - - - - - - - - - - - - - - - - - ------------

要跨浏览器,代码如下:

if(window.navigator.msSaveOrOpenBlob) {
    //IE11 & Edge
    window.navigator.msSaveOrOpenBlob(blobData, fileName); 
}
else{
   //Other browsers
    window.URL.createObjectURL(blobData);
    ...
}

你也可以参考我做的这个简单的例子:https://stackblitz.com/edit/angular-coueov