node 文件下载到本地

 

//导入
const path = require('path');
const fs = require('fs');

 function  downLoadTemplate() {
        try {
            const _path = path.join(__dirname, '../static/template.doc')
            const stats = fs.statSync(_path)
            if (stats.isFile()) {
                ctx.set({
                    'Content-Type': 'application/octet-stream',
                    'Content-Disposition': 'attachment; filename=template.doc',
                    'Content-Length': stats.size
                });
               fs.createReadStream(_path);
            } else {
     console.log("下载模版遇到错误")
            }
        } catch (ex) {
           console.log("下载模版遇到错误")
        }
    }

  

posted @ 2020-09-22 15:49  Fly_bokeyuan  阅读(733)  评论(0编辑  收藏  举报