【发布时间】:2020-12-17 08:41:33
【问题描述】:
我正在尝试创建一个包含一些 .txt 文件的存档,然后我想下载此存档。请看下面的代码:
async function archiveAndDownload(res) {
const bashCommand = ...
const archive = ...
exec(bashCommand, (err, stdout, stderr) => {
if (err && err.code != 1) {
console.log(err);
res.status(500).json({ error: `Error.` });
return;
} else {
if (stderr) {
console.log(stderr);
}
}
});
res.status(200).download(archive, async (err) => {
if (err) {
console.log("Cannot download the archive " + err);
} else {
fs.unlink(archive);
}
});
}
async function getX(req, res) {
try {
await archiveAndDownload(res);
} catch (err) {
console.log("Error: " + err);
}
}
当尝试从 Postman 测试它时,我收到此错误:
无法下载存档错误:请求中止
我该如何解决?感谢您的宝贵时间!
(顺便说一句,如果我尝试在else 上的 exec 中移动下载操作,它会起作用,但我希望有 2 个单独的代码块)
【问题讨论】:
标签: javascript node.js express request http-get