我正在做一个项目,其中一个 pdf 是在 Node 服务器上动态创建的,需要发送到 javascript 客户端,它应该在浏览器上打开一个下载提示。
我已经创建了 pdf,但发送和接收可下载文件很麻烦。
以下是我在 Node 上用于发送 pdf 的代码,但它似乎不正确:
var pdf = fs.readFile("createdPdf/" + uname + "_44.pdf", function() {
if (pdf == null) {
res.writeHead(401, {"Content-Type": "text/html"});
res.write('Failed');
res.end();
return;
} else {
res.writeHead(200, {
"Content-Type": "text/html"
});
res.write(pdf);
res.end();
}
});
而且我不知道如何在 javascript 客户端捕获此 pdf 以打开下载提示。
当前来自 Node 的所有其他响应都被收集为:
var resp_json = printRequest.getResponseJson();
或
var resp_json = printRequest.getResponseText();
谁能帮帮我?
附言我在客户端使用谷歌闭包库(不要问为什么,公司 MO)。
提前致谢!
最佳答案
尝试添加 Content-Disposition: 附件 header ,如下所示:
Content-Disposition: attachment; filename="thePdf.pdf"
关于javascript - 如何通过 node.js 将可下载的 pdf 发送到 javascript 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16978546/