如何解决koajs:如何等待客户端接收文件?
我想用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 举报,一经查实,本站将立刻删除。