Angular客户端代码:
$http.post('/zip', {
id: _id
})
.success(function (data, status, headers, config) {
var blob = new Blob([data], {type: "application/zip"});
var contentDisp = headers('content-disposition');
if (contentDisp && /^attachment/i.test(contentDisp)) {
var fileName = contentDisp.toLowerCase()
.split('filename=')[1]
.split(';')[0]
.replace(/"/g, '');
//The below command works but generates a corrupt zip file.
FileSaver.saveAs(blob, fileName);
}
})
.error(function () {
console.log("Could not download");
});
NodeJS服务器代码:
app.route('/zip/')
.post(function(req, res) {
var output = fs.createWriteStream(join(outdir, outzipfile));
//Using s3zip to archive.
s3Zip
.archive({ s3: s3Client, bucket: bucket}, folder, s3_files)
.pipe(output);
output.on('close', function() {
//This sends back a zip file.
res.download(outPutDirectory + outputBcemFile);
});
output.on('error', function(err) {
console.log(err);
return res.status(500).json({error: "Internal Server Error"});
});
});
虽然FileSaver.saveAs可以工作并下载zip文件,但它似乎已损坏。类型" application / zip" 是否正确?我也尝试了" octet / stream" ,它也下载了一个损坏的zip文件。任何帮助都将非常宝贵!感谢。
答案 0 :(得分:2)
这是下面链接中Git中提到的错误:
https://github.com/eligrey/FileSaver.js/issues/156
要解决此问题,您需要在$ http请求标头中添加:responseType:'arraybuffer',它将起作用。