Home > OS >  How to send file stream with file name and file extension using NodeJS
How to send file stream with file name and file extension using NodeJS

Time:02-18

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

CodePudding user response:

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