admin管理员组

文章数量:1399976

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);

本文标签: javascriptHow to send file stream with file name and file extension using NodeJSStack Overflow