我使用Easyzip生成了一个zip文件。我可以直接从下载文件夹中打开它。但是在下载后尝试打开它时,我收到此错误: - '提取文件时出错#39;。
这是我的后端代码: -
var zip2 = new EasyZip();
zip2.zipFolder('./downloads/'+application._id,function(){
zip2.writeToFile('./downloads/'+application._id+'.zip');
res.setHeader('Content-disposition', 'attachment; filename='+application._id+'.zip');
res.setHeader('Content-type', 'application/zip');
var fs = require('fs');
var filestream = fs.createReadStream('./downloads/'+application._id+'.zip');
filestream.pipe(res);
});
});
我的angular.js代码
$http.post('/download/archive/' + stateParams._id, {year: year}).success(function (data) {
var file = new Blob([data], {type: 'application/zip'});
console.log(file)
saveAs(file, 'application.zip');
});
请帮我解决这些问题。提前谢谢。
答案 0 :(得分:0)
我有同样的问题,并能够通过使用以下方法解决它: $ http.post()请求中的{responseType:' arraybuffer'}。 有关详情,请查看:how to download a zip file using angular以及AngularJS: Display blob (.pdf) in an angular app
答案 1 :(得分:0)
使用node-native-zip模块解决了问题。
这是我的后端代码
var zip = require("node-native-zip");
var files = []
files.push({name:application._id+'.pdf',path:'./downloads/'+vetting_id+application._id+'.pdf'}); //push all the files along with its name and path
var archive = new zip();
archive.addFiles(files, function (err) {
if (err) return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
var buff = archive.toBuffer();
var fs = require('fs');
fs.writeFile('./downloads/'+ vetting._id+'.zip', buff, function () {
console.log("Finished");
var filestream = fs.createReadStream('./downloads/'+vetting._id+'.zip');
filestream.pipe(res);
});