Skip to content

extend a method context.downloader to download file for egg

License

Notifications You must be signed in to change notification settings

zzzs/egg-downloader

Repository files navigation

egg-downloader

NPM version build status Test coverage David deps Known Vulnerabilities npm download

extend a method ctx.downloader to download file

Install

$ npm i egg-downloader --save

Usage

// {app_root}/config/plugin.js
exports.downloader = {
  enable: true,
  package: 'egg-downloader',
};

Features

ctx.downloader用于提供文件下载的功能,支持三个参数,ctx.download(file, [name], [header])

  • file: 必选,文件路径
  • name: 可选,下载文件名(包含后缀)
  • header: 可选,附加头信息, 并且header会改为全部小写(感谢天猪的建议:egg 推荐 header 全部小写,会有一定的性能提升)。

如果 file 是个不带路径的文件,将默认视为下载 app/public/file 。

Example

// {app_root}/controller/home.js
const Controller = require('egg').Controller;
class HomeController extends Controller {
  async index() {
    // 下载为 package.json
    this.ctx.downloader('./package.json');
    
    // 下载为 new-name.json
    // this.ctx.downloader('package.json', 'new-name.json');
    
    // 下载为 new-name.json, header: X-Token: 123
    // this.ctx.downloader('package.json', 'new-name.json', { 'x-token': '123' });
  }
}
module.exports = HomeController;

Questions & Suggestions

Please open an issue here.

License

MIT

About

extend a method context.downloader to download file for egg

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published