从 IE11 下载文档后无法加载 pdf 文档
Failed to load pdf document after downloading the document from IE11
我有一个 ajax 电话,我正在调用 JSReport 并下载 pdf 文档。在 chrome 和其他浏览器中,它可以正常工作。文档已下载,我可以打开它。但是在IE11下,文档下载好了,但是如果我想打开pdf文件,总是显示加载pdf文档失败。我在 IE11 控制台中调试了代码,但没有给出任何错误。
这是我的代码,
$.ajax({
url: 'my api url',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
type: 'POST',
data: JSON.stringify(selectedIds),
success: function (data) {
var a = document.createElement('a');
var isIE = false || !!document.documentMode;
if (!isIE) {
// This part is working properly
for (var i = 0; i < data.length; i++) {
a.href = "data:application/octet-stream;base64," + data[i];
a.target = '_blank';
a.download = 'report.pdf';
a.click();
}
} else {
// This part is for IE11 browser where I have problem to load the document after download
for (var i = 0; i < data.length; i++) {
var file = new Blob([data[i]], {
type: 'application/octet-stream'
});
window.navigator.msSaveOrOpenBlob(file, 'report.pdf');
}
}
},
fail: function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
}
});
终于,我找到了解决办法!我实际上需要将 base64 字符串数据转换为 Blob。然后我们就可以下载了。以前,它没有正确转换。这就是为什么文件已下载但由于损坏而无法加载的原因。下面是我的代码,
$.ajax({
url: 'my api url',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
type: 'POST',
data: JSON.stringify(selectedIds),
success: function (data) {
var a = document.createElement('a');
var isIE = false || !!document.documentMode;
if (!isIE) {
// This part is working properly
for (var i = 0; i < data.length; i++) {
a.href = "data:application/octet-stream;base64," + data[i];
a.target = '_blank';
a.download = 'report.pdf';
a.click();
}
} else {
for (var i = 0; i < data.length; i++) {
var base64Data = data[i];
var fileName = 'report.pdf';
var byteCharacters = atob(base64Data);
var byteNumbers = new Array(byteCharacters.length);
for (var j = 0; j < byteCharacters.length; j++) {
byteNumbers[j] = byteCharacters.charCodeAt(j);
}
var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([byteArray], { type: 'application/pdf' });
window.navigator.msSaveOrOpenBlob(blob, fileName);
}
}
},
fail: function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
}
});
我有一个 ajax 电话,我正在调用 JSReport 并下载 pdf 文档。在 chrome 和其他浏览器中,它可以正常工作。文档已下载,我可以打开它。但是在IE11下,文档下载好了,但是如果我想打开pdf文件,总是显示加载pdf文档失败。我在 IE11 控制台中调试了代码,但没有给出任何错误。 这是我的代码,
$.ajax({
url: 'my api url',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
type: 'POST',
data: JSON.stringify(selectedIds),
success: function (data) {
var a = document.createElement('a');
var isIE = false || !!document.documentMode;
if (!isIE) {
// This part is working properly
for (var i = 0; i < data.length; i++) {
a.href = "data:application/octet-stream;base64," + data[i];
a.target = '_blank';
a.download = 'report.pdf';
a.click();
}
} else {
// This part is for IE11 browser where I have problem to load the document after download
for (var i = 0; i < data.length; i++) {
var file = new Blob([data[i]], {
type: 'application/octet-stream'
});
window.navigator.msSaveOrOpenBlob(file, 'report.pdf');
}
}
},
fail: function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
}
});
终于,我找到了解决办法!我实际上需要将 base64 字符串数据转换为 Blob。然后我们就可以下载了。以前,它没有正确转换。这就是为什么文件已下载但由于损坏而无法加载的原因。下面是我的代码,
$.ajax({
url: 'my api url',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
type: 'POST',
data: JSON.stringify(selectedIds),
success: function (data) {
var a = document.createElement('a');
var isIE = false || !!document.documentMode;
if (!isIE) {
// This part is working properly
for (var i = 0; i < data.length; i++) {
a.href = "data:application/octet-stream;base64," + data[i];
a.target = '_blank';
a.download = 'report.pdf';
a.click();
}
} else {
for (var i = 0; i < data.length; i++) {
var base64Data = data[i];
var fileName = 'report.pdf';
var byteCharacters = atob(base64Data);
var byteNumbers = new Array(byteCharacters.length);
for (var j = 0; j < byteCharacters.length; j++) {
byteNumbers[j] = byteCharacters.charCodeAt(j);
}
var byteArray = new Uint8Array(byteNumbers);
var blob = new Blob([byteArray], { type: 'application/pdf' });
window.navigator.msSaveOrOpenBlob(blob, fileName);
}
}
},
fail: function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
}
});