Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send file stream with file name and file extension using NodeJS

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

like image 825
M.K. Malik Avatar asked Nov 29 '25 18:11

M.K. Malik


1 Answers

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);
like image 65
sina-heisenberg Avatar answered Dec 01 '25 07:12

sina-heisenberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!