最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send file stream with file name and file extension using NodeJS - Stack Overflow

programmeradmin5浏览0评论

I'm using nodejs to send file streams to express response

var fileReadStream = fs.createReadStream(filePath);
fileReadStream.pipe(res);

The problem is, on the front-end, the file is downloadable only with the "download", and without extension. And this makes the file unable to use (as no extension).

I'm using nodejs to send file streams to express response

var fileReadStream = fs.createReadStream(filePath);
fileReadStream.pipe(res);

The problem is, on the front-end, the file is downloadable only with the "download", and without extension. And this makes the file unable to use (as no extension).

Share Improve this question edited Feb 17, 2022 at 16:17 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Feb 17, 2022 at 11:22 M.K. MalikM.K. Malik 1931 gold badge2 silver badges11 bronze badges 1
  • 2 Adding Content-Disposition: attachment; filename="FILENAME" header to your response should do the trick. – Daniel Commented Feb 17, 2022 at 11:34
Add a ment  | 

1 Answer 1

Reset to default 8

please add this header before sending the response(replace the filename according your need):

 var fileReadStream = fs.createReadStream(filePath);  
 res.setHeader('Content-disposition', 'attachment; filename=YOUR_FILE.EXTENSION');
 fileReadStream.pipe(res);
发布评论

评论列表(0)

  1. 暂无评论