问题
我的PDF下载适用于Chrome,但在IE 11/10/9中无效。
在IE中,它会提示我一个安全警告,但是在我单击是以允许它之后,没有任何反应。
CODE
handleDownload = (reportLoc) => {
var path = reportLoc.item.ReportPath;
$.get("/Api/Apps/GetFileFromServer?filePath=" + path, (response) => {
this.setState({ base64EncodedPDF: response });
}).then(() => {
let a = document.createElement("a");
a.href = "data:application/octet-stream;base64," + this.state.base64EncodedPDF;
a.download = path.substring(path.lastIndexOf("\\") + 1);
a.click();
});
}
使其适用于Chrome和IE的解决方案?
答案 0 :(得分:0)
在查看以下2个参考文献后,我设法解决了这个问题:
https://blog.jayway.com/2017/07/13/open-pdf-downloaded-api-javascript/
Creating a Blob from a base64 string in JavaScript
我的代码现在:
handleDownload = (reportLoc) => {
var path = reportLoc.item.ReportPath;
$.get("/Api/Apps/GetFileFromServer?filePath=" + path, (response) => {
this.setState({ base64EncodedPDF: response });
}).then(() => {
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
var byteCharacters = atob(this.state.base64EncodedPDF);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([byteArray], { type: "application/octet-stream" });
window.navigator.msSaveOrOpenBlob(blob, path.substring(path.lastIndexOf("\\") + 1));
return;
}
let a = document.createElement("a");
a.href = "data:application/octet-stream;base64," + this.state.base64EncodedPDF;
a.download = path.substring(path.lastIndexOf("\\") + 1);
a.click();
});
}