我尝试使用以下代码将文件发送到客户端:

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

它工作正常,但是当用户从url下载此文件时,我需要这样做:
http://mynodejssite.com/get/myfile

浏览器中的文件名必须是“other_file_name.dat”,而不是“myfile”。

最佳答案

有一种专门的方法res.download

它为您覆盖了所有;)

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

关于node.js - 下载中带有文件名的NodeJS sendFile,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41941724/

10-14 19:40
查看更多