BACKEND CALL
router.post('/exportZip', async function (req, res) {
const source = path.join('./publics/', req.body.id);
const out = './publics/' + req.body.id + '.zip';
const archive = archiver('zip', { zlib: { level: 0 } });
const stream = fs.createWriteStream(out);
return new Promise((resolve, reject) => {
archive
.directory(source, false)
.on('error', err => reject(err))
.pipe(stream)
;
stream.on('close', () => {
resolve()
});
archive.finalize().then(function () {
res.status(200).download(path.join(__dirname, '.' + out), function (err) {
fs.unlink(out, function (err) {
if (err) console.log(err);
})
});
});
});
});
语言服务
exportSubmissionsAsZip(contest){
return this.http.post('/exportZip', {id:_id}, {
responseType: 'arraybuffer',
headers: new HttpHeaders({ 'Content-Type': 'application/json'})
});
}
角质前缘
downloadSubmissions(event){
this.contest.exportSubmissionsAsZip(event).subscribe(res =>{
console.log(res);
var blob = new Blob([res], {type: "application/zip"})
saveAs(blob, event._id+'.zip')
})
}
所以从服务器上下载软件包时,我莫名其妙地得到了损坏的软件包,
希望有人可以帮助我解决这个问题,因为到目前为止我所做的更改都没有关系。问题保持不变。
更新:
由于某种原因,数组缓冲区大于文件本身