问题描述
我想用koa(2)返回一个生成的文件,客户端收到文件后,应该删除该文件。
// this function is called by Koa
async myMethod (ctx) {
const file = await something()// generate a file
ctx.attachment(path.basename(file))
ctx.body = fs.createReadStream(file)
// how can I await here for the client to fully receive file?
await fs.promises.unlink(file)
}
我可以这样做:
ctx.body.on('end',async () => {
console.log('client finished receiving')
await fs.promises.unlink(file)
})
但我宁愿将删除逻辑放在一个单独的中间件中, 因此我必须等待文件完全发送才能从函数返回
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)