node多种方法实现文件下载
发布于 11 年前 作者 201030560116 18200 次浏览 最后一次编辑是 8 年前 来自 问答
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 回复

代码乱就乱吧,可是感觉都不全啊,还是我太菜了。。。

垃圾东西,代码都不全。

回到顶部