在 express 中 res.download 时标头已发送错误

问题描述

 res.download(file_path,"CV.pdf",function (err) {
    if (err) {
      // Handle error,but keep in mind the response may be partially-sent
      // so check res.headeRSSent
      if (res.headeRSSent) {
        return console.log(res.headeRSSent);
      } else {
        return res.status(500).json({ message: "Downloaded unsuccessfully",errors: err });
      }
    } else {
      return res.status(200).json({ message: `Downloaded ${filename} successfully`,errors: false });
    }
  });

通常,阅读文档时,如果第一个 if 块中出现错误,则标头可能已经发送。但是,我的错误来自在最后一个 return 语句中没有错误对象时。如果我重新启动服务器,我可以下载一次文件,然后它会崩溃。不知道为什么会这样。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)