koa-send实现文件下载

文件结构

1、app.js相关文件引入

const koa = require('koa'); // "koa": "^2.2.0"
const app = new koa();
const router = require('koa-router')(); // "koa-router": "^7.2.0"
const send = require('koa-send'); // "koa-send": "^4.1.0"

router.get('/', async function (ctx) {
    var fileName = 'index.html';
    await send(ctx, fileName, { root: __dirname + '/public' });
});

router.get('/download', async function (ctx) {
    // 为了方便演示,这里直接下载index页面
    var fileName = 'index.html';
    // Set Content-Disposition to "attachment" to signal the client to prompt for download.
    // Optionally specify the filename of the download.
    // 设置实体头(表示消息体的附加信息的头字段),提示浏览器以文件下载的方式打开
    // 也可以直接设置 ctx.set("Content-disposition", "attachment; filename=" + fileName);
    ctx.attachment(fileName);
    await send(ctx, fileName, { root: __dirname + '/public' });
});

app
    .use(router.routes())
    .use(router.allowedMethods());

app.listen(3000);

2、测试页面代码: index.html





    
    
    
    __小简__下载测试
    
    



    

2、测试开始:浏览器打开http://localhost:3000/

koa-send实现文件下载_第1张图片
点击下载就可以看到效果了
koa-send实现文件下载_第2张图片
效果图

你可能感兴趣的:(koa-send实现文件下载)