【发布时间】:2020-12-16 05:29:12
【问题描述】:
我有以下快递请求回调
zip.addLocalFolder(`/path/to/folder`, `./`);
var data = zip.toBuffer();
fs.writeFile(`path/to/download.zip`,data,function (err) {
if (err) return console.log(err);
res.download(`path/to/download.zip`)
});
调用回调函数后fs.writeFile似乎正在写入文件。
编辑:文件写入成功。事实上,它是在我执行res.download() 之后编写的,导致错误
如果我在 setTimeout 1 秒内调用res.download(),则执行成功结束。
我收到此错误:
ENOENT:没有这样的文件或目录,stat 'path/to/download.zip`
把代码改成
zip.addLocalFolder(`/path/to/folder`, `./`);
var data = zip.toBuffer();
fs.writeFileSync(`path/to/download.zip`,data);
res.download(`path/to/download.zip`);
具有相同的效果。
我使用的库 adm-zip 有一个编写 zip 文件的方法,并且使用它具有相同的效果。
我有什么遗漏吗?
【问题讨论】:
-
将
fs.writeFileAsync(path/to/download.zip,data);替换为fs.writeFileSync(path/to/download.zip,data);检查此 link 以在 nodejs 中同步文件写入。跨度> -
@LilFlower ,事实并非如此。它仍然会失败,因为问题来自文件的不正确/不存在的路径。
-
@LilFlower 你是对的,我的错,尽管它仍然失败
标签: javascript node.js express