NodeJS sendFile with File Name in download

Massimo Caroccia picture Massimo Caroccia · Jan 30, 2017 · Viewed 24.8k times · Source

I try to send file to client with this code:

router.get('/get/myfile', function (req, res, next) {
  res.sendFile("/other_file_name.dat");
});

it's work fine but I need that when user download this file from the url:

http://mynodejssite.com/get/myfile

the filename into the browser must be "other_file_name.dat" and not "myfile".

Answer

Michal Miky Jankovský picture Michal Miky Jankovský · Oct 13, 2017

there is a specialized method res.download

which covers all for you ;)

router.get('/get/myfile', function (req, res) {
    res.download("/file_in_filesystem.dat", "name_in_browsers_downloads.dat");
});