Express框架下载文件的方法,我想已经有人已经知道了。
这里说下Koajs的方法。
首先设置Content-disposition
let filename = 'xxxx';
ctx.set('Content-disposition', 'attachment; filename=' + filename + '.pdf');//attachment
或者
ctx.set('Content-disposition', 'inline; filename=' + filename + '.pdf');//inline
以上两种的区别是一个是attachment,意思就是附件,还有一种是inline,意思就是内附。区别就是attachment打开的时候可以下载文件,inline有时候可以下载,有时候可以直接浏览,好像跟浏览器有关。
然后设置下文件类型
ctx.set('Content-type', 'application/pdf');
给body赋值,这里是一个Buffer。
然后把文件内容读出来
let gReadData = new Buffer();//这里自己根据自己的具体情况去实现就好了。
在函数最后
return ctx.body = gReadData;
整体代码大概如下:
module.exports.downloadPdf = async (ctx,next) => {
let filename = 'xxxx';
ctx.set('Content-disposition', 'attachment; filename=' + filename + '.pdf');
ctx.set('Content-type', 'application/pdf');
return ctx.body = gReadData;
}