node.js - 为什么在客户端打开 pdf 时无法打开文档错误或空白 pdf?
问题描述
我在响应类似于http://www.orimi.com/pdf-test.pdf的 get api 中需要一些秘密信息,这些信息仅在用节点编写的中间件中可用。我想在客户端(浏览器)上打开 pdf。所以我点击了代理get api,它将点击中间件,中间件将点击后端服务器,但我无法打开文档和空白pdf。谁能告诉我这段代码有什么问题?
fetch(pdfApiMiddlewareUrl, {
method: "GET",
headers: {
"Content-Type": "application/pdf",
'Content-Disposition': 'inline; filename=your_file_name'
},
responseType : 'blob'
})
.then(res => res.blob())
.then(response => {
var blobUrl = URL.createObjectURL(response);
window.open(blobUrl);
})
.catch(error => {
console.log("HEREEEEEEEE");
console.log(error);
});
中间件代码:
var urlEndPointsToHit = decodeURIComponent(req.query.urlToHit);
var url = urlEndPointsToHit+'?secret='+secretInfo;
var options;
options = {
url: url,
method: 'GET',
headers: {
'Content-type': 'application/pdf'
},
};
if(options) {
options.qs = req.query || {};
}
request(options, function(err, resp, body) {
req.locals = body;
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Cache-Control', 'no-cache');
next();
});
解决方案
推荐阅读
- c# - 如何在服务器端设置没有 id 属性的 Input 元素的值?
- apache-spark - 如何在不使用 sparkListener 操作的情况下为不同的数据帧写入记录计数?
- node.js - 无法在启用了 Istio 的 GKE 集群中的 2 个节点、js 应用程序之间进行通信
- java - Android - 将 MediaMuxer 与 MediaExtractor 和 PCM 流一起使用会导致视频帧损坏
- python-3.x - ImportError:无法导入名称“extract_zipped_paths”
- python - 如何使用 google api 和 python 在 google docs 中打开文件?
- reactjs - 使用 axios 仅适用于邮递员
- clojure - Recursively calling function with specific keyword in Clojure
- ruby - 如何在给定全名数组的情况下有效地生成名字和姓氏的唯一组合
- postgresql - 如何在 Postgresql 中获取当前月份的字符串和年份