【发布时间】:2017-08-16 06:06:11
【问题描述】:
我目前正在调用外部 API,以便将工资单发送给我(以二进制形式发送到我的节点服务器,因为我必须在节点端使用证书身份验证)
我已经设法弄清楚如何让我的客户通过将其作为二进制对象发送到 chrome 上下载它,然后使用 createObjectURL 加载它,但这不适用于任何 IE 版本。
我尝试了各种解决方案,但似乎无法获得我所追求的兼容性,我想知道是否有人能够就我在客户端确保 IE8 兼容性的选项提供建议 > 服务时PDF?
服务器当前正在像这样发送接收到的 PDF 数据
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);
}
谢谢!
【问题讨论】:
-
您是否在客户端的输出流上设置了
Content-Type标头? -
客户端为什么要使用ie8?
-
@guest271314 你告诉我.. 我所知道的是我们必须支持锁定在 IE8 上的办公室,不幸的是它超出了我的控制范围
-
@thinice 我已经尝试了各种标题,我确定它们都正确设置了。我正在努力解决的是如何在所有浏览器的客户端上打开这些。下载永远不会按照当前的设置方式自动开始。我已经包含了来自服务器的代码
-
您是要将其嵌入网页还是下载?您能否详细说明您是如何尝试将其用作客户端的?
标签: javascript node.js pdf