zip并从nodejs下载

时间:2015-06-17 11:49:40

标签: node.js express download

我正在尝试压缩文件并使用easy-zip模块从服务器下载。但现在我可以使用以下代码写入服务器,但我怎样才能使其可下载???

var app = require('express')();
var easyzip = require('easy-zip');

app.get('/api/downloadFile',function(req,res){
    console.log("inside req");
    var data = "<html><body><h1>Inside new Html</h1></body></html>";
    var zip2 = new easyzip.EasyZip();
    var jsFolder = zip2.folder('data');
    jsFolder.file('app.js','alert("hello world")');
    jsFolder.file('index.html',data);
    zip2.writeToFile('folder.zip');
});

2 个答案:

答案 0 :(得分:3)

尝试使用zip2.writeToResponse(res,'folder.zip');代替zip2.writeToFile('folder.zip');

答案 1 :(得分:0)

而不是写入文件写入响应

var app = require('express')();
var easyzip = require('easy-zip');

app.get('/api/downloadFile',function(req,res){
    console.log("inside req");
    var data = "<html><body><h1>Inside new Html</h1></body></html>";
    var zip2 = new easyzip.EasyZip();
    var jsFolder = zip2.folder('data');
    jsFolder.file('app.js','alert("hello world")');
    jsFolder.file('index.html',data);
    zip2.writeToResponse(res,'folder');
    res.end();
})