typescript - 使用 node js 下载文件
问题描述
我正在使用带有节点 js 的打字稿并尝试将下载添加到服务器。我已将上传的文件保存在 public/data/uploads 文件夹中。
export const downloadFile: RequestController = (req, res) => {
try {
const fileName = req.params.name;
console.log(path.join(__dirname));
const directoryPath = "../../public/data/uploads";
return res.download(directoryPath + fileName, fileName, (err) => {
if (err) {
res.status(500).send({
message: "Could not download the file. " + err,
});
}
});
} catch (error) {
console.log(error);
return res.status(500).json("Internal server error");
}
};
每当我尝试运行这个控制器时,它都会抛出一个错误说
"message": "Could not download the file. Error: ENOENT: no such file or directory, stat '/home/bishal/Projects/Ticketing System/backend/build/controllers/public/data/uploads2006222-A-A-Milne-Quote-Promise-me-you-ll-always-remember-You-re-braver.jpg'"
那么如何在这里正确地给出文件夹方向。我的公用文件夹在构建文件夹之外
我怎样才能到达
解决方案
推荐阅读
- javascript - 获取上周日期范围从星期日到星期六的时刻js
- hibernate - 这些接口方法的实现在哪里?
- ionic-framework - Ionic 3 PWA 应用程序断点和浏览器中的源代码搞砸了
- cakephp - 使用自定义连接创建模式中的模型数据
- sql - 如何与列名一起获得一行中的最大值
- java - “java_client”更新后无法点击组合键
- java - 如何在 Openlayers 上找到多边形从中心的偏移值
- swift - 快速关闭当前视图控制器下方的所有视图控制器
- tkinter - 通过类在tkinter中打开图片
- facebook-instant-articles - 无效范围:manage_pages - Instant Articles