Javascript 将PDF作为BLOB发送到客户端,但缺少浏览器支持
我目前正在调用一个外部API,以便获得发送给我的工资单(以二进制形式发送到我的节点服务器,因为我必须在节点端使用证书身份验证) 我已经设法弄明白了如何让我的客户端在chrome上下载它,方法是将它作为二进制对象发送,然后使用createObjectURL加载它,但是这在任何IE版本上都不起作用 我尝试过各种解决方案,但似乎无法获得我想要的兼容性,我想知道在提供PDF时,是否有人能够就我在客户端确保IE8兼容性>的选项提供建议 服务器当前正在发送接收到的PDF数据,如下所示Javascript 将PDF作为BLOB发送到客户端,但缺少浏览器支持,javascript,node.js,pdf,Javascript,Node.js,Pdf,我目前正在调用一个外部API,以便获得发送给我的工资单(以二进制形式发送到我的节点服务器,因为我必须在节点端使用证书身份验证) 我已经设法弄明白了如何让我的客户端在chrome上下载它,方法是将它作为二进制对象发送,然后使用createObjectURL加载它,但是这在任何IE版本上都不起作用 我尝试过各种解决方案,但似乎无法获得我想要的兼容性,我想知道在提供PDF时,是否有人能够就我在客户端确保IE8兼容性>的选项提供建议 服务器当前正在发送接收到的PDF数据,如下所示 res.setHead
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=myPdf.pdf');
res.write(pay_pdf_data, 'binary');
res.end();
客户端上的当前方法在chrome上运行良好,但在IE/旧浏览器中不起作用
function httpGetAsync(theUrl, data, callback) {
var request = new XMLHttpRequest();
request.open("POST", theUrl, true);
request.responseType = "blob";
request.onload = function (e) {
if (this.status === 200) {
// `blob` response
console.log(this.response);
// create `objectURL` of `this.response` : `.pdf` as `Blob`
var file = window.URL.createObjectURL(this.response);
var a = document.createElement("a");
a.href = file;
a.download = this.response.name || "detailPDF";
document.body.appendChild(a);
a.click();
// remove `a` following `Save As` dialog,
// `window` regains `focus`
window.onfocus = function () {
document.body.removeChild(a)
}
};
};
request.setRequestHeader("Content-Type", "application/json");
request.send(data);
}
谢谢 IE不支持锚定标记上的
下载属性:
对于IE10/IE11,您可以使用window.navigator.msSaveOrOpenBlob
,但对于IE8/IE9,您需要添加一个包含页面上不可见iframe的破解:
<iframe name="seekritDownload" style="display:none"></iframe>
有关参考,请参阅:
编辑:除了缺少download
属性外,IE8还缺少XMLHttpRequest.responseType
和Blob
对象。我用iframe
添加了一个巨大的hack,应该可以工作。如果愿意,您可以使用JavaScript创建并附加iframe
元素您是否在客户端的输出流上设置内容类型
头?客户端为什么要使用ie8?@guest271314您告诉我。。我只知道我们必须支持锁定在IE8上的办公室不幸的是,这不在我的权限之内control@thinice我已经尝试了各种标题,我确定它们都设置正确。我正在努力解决的是如何在所有浏览器的客户端上打开这些。下载不会以当前设置方式自动启动。我已经包含了服务器上的代码。您是想将其嵌入网页还是下载?您能否详细说明如何将其用作客户端?Ie8也不支持XMLHttpRequest中的responseType。你抄袭了这个答案吗?不,我从OP逐字拷贝了JavaScript。我不认为他可能从别处复制了它。无论哪种方式,你都在ie8上尝试过吗?你在XMLHttpRequest上检查过ie8是否支持responseType吗?@charmeleon感谢你的回复,对我周末不在的回复表示歉意。这段代码在最新的chrome/IE/Firefox中有效,但在任何function httpGetAsync(theUrl, data, callback) {
var request = new XMLHttpRequest();
request.open("POST", theUrl, true);
// IE10/IE11 and other modern browsers
if ('responseType' in request) {
request.responseType = "blob";
request.onload = function (e) {
if (this.status === 200) {
// `blob` response
console.log(this.response);
// IE10/IE11
if ('msSaveOrOpenBlob' in window.navigator) {
// Here I assume `this.response` is the blob
window.navigator.msSaveOrOpenBlob(this.response, this.response.name || "detailPDF");
}
else {
// create `objectURL` of `this.response` : `.pdf` as `Blob`
var file = window.URL.createObjectURL(this.response);
var a = document.createElement("a");
a.href = file;
a.download = this.response.name || "detailPDF";
document.body.appendChild(a);
a.click();
// remove `a` following `Save As` dialog,
// `window` regains `focus`
window.onfocus = function () {
document.body.removeChild(a)
}
}
}
};
request.setRequestHeader("Content-Type", "application/json");
request.send(data);
}
else {
// This should work for IE8/IE9
window.open(theUrl, 'seekritDownload');
}
}