Created
December 26, 2017 23:50
-
-
Save kiramishima/2830edbd984223538033c68024ba047d to your computer and use it in GitHub Desktop.
Sending Files with Koa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myRoute.get("/download", async function (ctx) { | |
try { | |
const stream = fs.createReadStream(path.resolve(__dirname, '../public/img/350x150.png')); | |
// ctx.type = 'image/png'; | |
// with attachment | |
// ctx.attachment(path.resolve( __dirname, '350x150.png')); | |
ctx.set('Content-disposition', 'attachment; filename= file.png'); | |
ctx.body = stream; | |
} catch (ex) { | |
log.error(ex); | |
ctx.status = 400; | |
ctx.body = {success: false, message: ex.message}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment