arisalexis arisalexis - 3 months ago 25
Node.js Question

Rename a stream in node.js before sending it to client

I have the following code in an expressjs route:

readstream.on('error', function (err) {
console.log('An error occurred!', err);
throw err;
});

readstream.pipe(res);


I want to change the filename because currently it is a random id (it's stored like that in my database).

Answer

Set a header before sending it --

res.header('Content-Disposition', 'attachment; filename="new file name.pdf"');