nodejs实现文件下载,nodejs做为服务器,使用http模块,实现客户端浏览器下载效果



http.createServer(function(req, res) {

if(req.url=="/download"){
var path=__dirname+"/upload/J3FFpZulfF1BNEHAeJ0JBGHL.zip"; //要下载的文件
var fs = require("fs");
var f = fs.createReadStream(path);
res.writeHead(200, {
'Content-Type': 'application/force-download',
'Content-Disposition': 'attachment; filename=NW.zip'
});
f.pipe(res);
}

}