响应来自缓冲区的文件,请求两次要下载
我有一个Express Nodejs服务器,该服务器可以进行一些计算并创建PDF文件并将其作为响应发送。问题是,当用户试图下载文件时,端点被浏览器两次调用,因此计算完成了两次,并且需要两倍的时间,并且要回答请求大约需要20秒。我检查了一下,发现终点本身被称为两次不是favicon.ico或其他东西。
该文件未保存在服务器上,并以缓冲区的形式创建,然后将其管道输送到客户端。端点不是由前端代码调用,也不会由某物提取,它只是在浏览器的新选项卡中打开。下面列出了一些代码:
let buffer = await createPdfBuffer() // a custom function that creates the pdf as buffer
const readStream = new stream.PassThrough();
readStream.end(buffer);
res.set('Content-disposition', 'attachment; filename=' + file.pdf);
res.set('Content-Type', 'application/pdf');
readStream.pipe(res);
I have an express nodejs server which does some calculations and creates a pdf file and sends it as the response. The problem is, when the user tries to download the file, the endpoint is called twice by the browser so the calculations are done twice and it will take twice as long and it will take around 20 seconds for the request to be answered. I checked and saw that the endpoint itself is called twice not favicon.ico or other things.
The file is not saved on the server and is created as buffer and then piped to the client. The endpoint is not called by the frontend code or fetched by something, it is just opened in a new tab in the browser. Some of the code is listed below:
let buffer = await createPdfBuffer() // a custom function that creates the pdf as buffer
const readStream = new stream.PassThrough();
readStream.end(buffer);
res.set('Content-disposition', 'attachment; filename=' + file.pdf);
res.set('Content-Type', 'application/pdf');
readStream.pipe(res);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论