![](/img/trans.png)
[英]How to fetch remote PDF from a url and send to the client with Express js?
[英]Send pdf via express to js client and download
我正在尝试通过客户端从客户端的快递服务器发送pdf文件,如下所示:
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=test.pdf');
fs.createReadStream('test.pdf').pipe(res);
然后在客户端,我尝试通过将结果字符串转换为url并从那里下载来下载它。
var blob = new Blob[pdfString], { type: 'application/pdf' });
var url = window.URL;
var downloadUrl = url.createObjectURL(blob);
但是,生成的文件是两个空白页,我相信这可能是因为生成的文件的url大吗? 如果有人能找出问题出在哪里,或者告诉我一种更好的方法,那真是太好了。
我能够使用XMlHttpRequest弄清楚:
var req = new XMLHttpRequest();
req.open('GET', path , true);
req.responseType = "arraybuffer";
req.onload = (event) => {
downloadPdf(req.response); //This is where I convert to blob
}
req.send();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.