'How to pass value using variable in res.setHeader express.js

Trying to send file on the frontend with its name and extension.

var fileReadStream = fs.createReadStream(filePath);
res.setHeader("Content-disposition", `attachment; filename=${fileName}`);
fileReadStream.pipe(res);

On the frontend, getting ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION

How can I send the file name with its extension to download on the frontend?



Solution 1:[1]

You need to set the header like this

Content-Disposition: attachment; filename="filename.jpg"

In your case you should do it like below

var fileReadStream = fs.createReadStream(filePath);
res.setHeader(`Content-Disposition: attachment; filename=${fileName}`);
fileReadStream.pipe(res);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tauqeer Nasir