【问题标题】:Why is res.download() not working when passing in file path?为什么传入文件路径时 res.download() 不起作用?
【发布时间】:2021-08-14 18:51:24
【问题描述】:

大家好,我正在尝试创建一个功能,允许用户输入文件路径并下载它。我正在使用输入文本类型的表单来获取路径。我正在使用 Node.js 和 Express 路由器来处理获取和发布请求。由于某种原因,我的 res.download 无法正常工作。

router.get('/downloadFiles', (req, res, next) => {
  const filePath = req.query.filePath;
  res.download(filePath);
  res.redirect('/');
});

【问题讨论】:

  • 你检查过路径上的文件是否存在,路径是否正确?
  • Console.log(filePath) 并检查资源是否确实存在。
  • 另外,res.download() 之后的 res.redirect() 不应该工作。您没有收到标头已发送错误吗?

标签: node.js rest express https


【解决方案1】:

删除后 res.redirect('/'); 行它工作

【讨论】:

    猜你喜欢
    • 2013-01-31
    • 2021-08-09
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多