将zip文件发送到有角度的前端时损坏

时间:2019-05-28 15:42:15

标签: node.js angular express archiverjs

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')
    })
  }

所以从服务器上下载软件包时,我莫名其妙地得到了损坏的软件包,

enter image description here

希望有人可以帮助我解决这个问题,因为到目前为止我所做的更改都没有关系。问题保持不变。

更新

由于某种原因,数组缓冲区大于文件本身

enter image description here enter image description here

添加响应观察者后的更新: enter image description here

0 个答案:

没有答案