如何解决此错误;“访问被拒绝”;在IE浏览器中从angular7应用程序下载PDF时?
在我的angular 7应用程序中,我有一个API,当调用它时,它将下载一个PDF 以下是我获取PDF的方法:如何解决此错误;“访问被拒绝”;在IE浏览器中从angular7应用程序下载PDF时?,pdf,internet-explorer,angular7,Pdf,Internet Explorer,Angular7,在我的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(
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浏览器中运行良好,我可以点击链接并将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,称为msSaveBlob
或msSaveOrOpenBlob
msSaveBlob
和msSaveOrOpenBlob
方法之间的区别在于前者仅向用户提供保存按钮,而后者同时提供保存和打开按钮。您可以在IE中这样使用它们:
window.navigator.msSaveOrOpenBlob(blobData, fileName);
有关更多信息,请参阅和
---------------------------------------------------编辑-------------------------------------------------------
要使其跨浏览器,代码如下所示:
if(window.navigator.msSaveOrOpenBlob) {
//IE11 & Edge
window.navigator.msSaveOrOpenBlob(blobData, fileName);
}
else{
//Other browsers
window.URL.createObjectURL(blobData);
...
}
您也可以参考我制作的这个简单示例:IE不支持在获取PDF的方法中使用的
URL.createObjectURL()
。IE有自己的用于创建和下载文件的API,称为msSaveBlob
或msSaveOrOpenBlob
msSaveBlob
和msSaveOrOpenBlob
方法之间的区别在于前者仅向用户提供保存按钮,而后者同时提供保存和打开按钮。您可以在IE中这样使用它们:
window.navigator.msSaveOrOpenBlob(blobData, fileName);
有关更多信息,请参阅和
---------------------------------------------------编辑-------------------------------------------------------
要使其跨浏览器,代码如下所示:
if(window.navigator.msSaveOrOpenBlob) {
//IE11 & Edge
window.navigator.msSaveOrOpenBlob(blobData, fileName);
}
else{
//Other browsers
window.URL.createObjectURL(blobData);
...
}
您也可以参考我制作的这个简单示例:我尝试了您提出的建议,但是,我遇到了这个错误:
core.js:15724错误类型错误:window.navigator.msSaveOrOpenBlob不是一个函数
我在代码中替换了这一行:downloadLink.href=window.URL.createObjectURL(新Blob(binaryData,{type:dataType}];
用这一行:downloadLink.href=window.navigator.msSaveOrOpenBlob(新Blob(binaryData,{type:dataType}));
请不要仅仅替换它,因为除了IE 11和Edge之外,其他浏览器不支持msSaveOrOpenBlob
。如果在其他浏览器中使用它,它可能会显示错误“不是一种功能“。你应该检查IE或其他浏览器的条件。我已经更新了我的答案。我尝试了你提出的建议,但是,我收到了以下错误:core.js:15724错误类型错误:window.navigator.msSaveOrOpenBlob不是函数
我在代码中替换了这一行:downloadLink.href=window.URL.createObjectURL。”(新Blob(binaryData,{type:dataType}));
这一行:downloadLink.href=window.navigator.msSaveOrOpenBlob(新Blob(binaryData,{type:dataType}))
请不要只是替换它,因为除了IE 11和Edge之外,其他浏览器不支持MSSAVEOROPENBLOBOB
。如果在其他浏览器中使用它,它可能会显示错误“not a function”。您应该检查IE或其他浏览器的条件。我已经更新了答案。