Skip to content

Instantly share code, notes, and snippets.

@kiramishima
Created December 26, 2017 23:50
Show Gist options
  • Save kiramishima/2830edbd984223538033c68024ba047d to your computer and use it in GitHub Desktop.
Save kiramishima/2830edbd984223538033c68024ba047d to your computer and use it in GitHub Desktop.
Sending Files with Koa
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