Answer the question
In order to leave comments, you need to log in
How to perform file transfer after 'close' event in koajs?
It is necessary to send the file to the client after the completion of all operations with it.
Here's what I'm trying to do:
async create(ctx) {
ctx.attachment(filename);
ctx.set('Content-Type', mimetype);
const arch = fs.createWriteStream(path.join(__dirname, 'someArchive.zip'));
ctx.body = arch.on('close', () => {
console.log('writeStream was closed');
fs.createReadStream(file);
});
},
Answer the question
In order to leave comments, you need to log in
I'll answer my own question :)
app.use(async ctx => {
await new Promise((resolve, reject) => {
const stream = fs.createWriteStream(...)
stream.on('close', () => {
ctx.body = fs.createReadStream(...)
resolve()
})
})
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question