网页(前端)正在调用服务,该服务发送PDF流作为响应:这是前端代码:
'click .btn': function (event) {
/.../
event.preventDefault();
Http.call(params, (err, res) => { // callback
if (err) console.log(err); // nothing
console.log({ res }); // print below result
const blob = new Blob(
[res.content],
{ type: `${res.headers['content-type']};base64` }
);
saveAs(blob, res.headers['content-disposition'].slice(21));
});
}
Run Code Online (Sandbox Code Playgroud)
这是来自服务器的响应(console.log(res)):{res:Object}打印在控制台中。
content: "%PDF-1.4?1 0 obj?<<?/Title (??)?/Creator (??)?/Prod ..... lot of characters....%"
data: null,
statusCode: 200,
headers: {
connection: "close",
content-disposition: "attachment; filename=myDoc.pdf"
content-type: "application/pdf",
date: "date",
transfer-encoding: "chunked",
x-powered-by: "Express"
}
Run Code Online (Sandbox Code Playgroud)
但是,下载的PDF没有内容,它像损坏的全空白(但是我可以在字符串中看到内容)。它与CSV路由配合得很好(我将csv作为流发送并以相同的方法下载并获得了数据)。
我认为有些格式为%PDF ...%,但是我没有找到任何内容。注意:使用邮递员,它可以工作,我的PDF已保存,页面不空白,我得到了数据。所以前面有件事我做错了。我也尝试过:
const fileURL = URL.createObjectURL(blob); …
Run Code Online (Sandbox Code Playgroud)