node多种方法实现文件下载
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();
});*/
};
3 回复
代码乱就乱吧,可是感觉都不全啊,还是我太菜了。。。
m
垃圾东西,代码都不全。