node多种方法实现文件下载
发布于 2年前 作者 201030560116 708 次浏览

exports.download = function(req,res){ var downloadPath = Config.outName; console.log('downloadPath=’+downloadPath); res.download(downloadPath); //express自带下载 /path.exists(downloadPath, function(exists) { console.log("exists: ", exists); if (exists) { var fileStream = fs.createReadStream(downloadPath); res.writeHead(200, {"Content-Type":"application/octet-stream"}); fileStream.pipe(res); console.log(‘fileStream pipe’); fileStream.on("end", function() { res.end(); }) } });/ /*var filename = path.basename(downloadPath); var mimetype = mime.lookup(downloadPath); //匹配文件格式

res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);

var filestream = fs.createReadStream(downloadPath);
filestream.on('data', function(chunk) {
    res.write(chunk);
});
filestream.on('end', function() {
    res.end();
});*/

};

回到顶部