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)

    },

猜你喜欢

转载自www.cnblogs.com/czq-0214/p/9139456.html
今日推荐