通过Express发送pdf到js客户端并下载 [英] Send pdf via express to js client and download
本文介绍了通过Express发送pdf到js客户端并下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我这样请求时,我正尝试使用客户端从快递服务器发送pdf文件:
I am trying to send pdf file from my express server using the client when requested like this:
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'attachment; filename=test.pdf');
fs.createReadStream('test.pdf').pipe(res);
然后在客户端,我尝试通过将结果字符串转换为url并从那里下载来下载它.
And then on the client side I am trying to download it by taking the resulting string converting it to a url and downloading from there.
var blob = new Blob[pdfString], { type: 'application/pdf' });
var url = window.URL;
var downloadUrl = url.createObjectURL(blob);
但是生成的文件有两个空白页,我相信这可能是因为生成的文件的url太大了吗?如果有人能找出问题出在哪里,或者告诉我一种更好的方法,那真是太棒了.
However the resulting file is two empty pages and I beleive think it might be because the resulting file is url is to large? If anyone could figure out whats wrong here or tell me of a better way to do it that would be awesome.
推荐答案
我能够使用XMlHttpRequest找出答案:
I was able to figure it out using 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();
这篇关于通过Express发送pdf到js客户端并下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文