node 文件下载到本地

Posted czq_0214

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node 文件下载到本地相关的知识,希望对你有一定的参考价值。

 downloadfile:function(req,res){
        var path= req.query.filename;

        setTimeout(function(){
            var filename = path.split(‘/file‘)[1].substr(1,path.split(‘/file‘)[1].length)
            var filePath =config.targetDir+ filename;
            var stats = fs.statSync(filePath);
            if(stats.isFile()){
                res.set({
                    ‘Content-Type‘: ‘application/octet-stream‘,
                    ‘Content-Disposition‘: ‘attachment; filename=‘+filename,
                    ‘Content-Length‘: stats.size
                });
                fs.createReadStream(filePath).pipe(res);
            } else {
                res.end(404);
            }
        },1000)

    },

 

以上是关于node 文件下载到本地的主要内容,如果未能解决你的问题,请参考以下文章