node.jsexpresssendfile

NodeJS sendFile with File Name in download


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


Solution

  • 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");
    });