0

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

    });
});

});

ANGULAR SERVICE

exportSubmissionsAsZip(contest){
    return this.http.post('/exportZip', {id:_id}, {
      responseType: 'arraybuffer',
      headers: new HttpHeaders({ 'Content-Type': 'application/json'})
    });
  }

ANGULAR FRONTEND

downloadSubmissions(event){
    this.contest.exportSubmissionsAsZip(event).subscribe(res =>{
      console.log(res);
      var blob = new Blob([res], {type: "application/zip"})
      saveAs(blob, event._id+'.zip')
    })
  }

So somehow I get corrupted packages when I download it from my server,

enter image description here

hopefully, someone can help me with this issue because it doesn't matter what I changed so far. the problem stayed the same.

Update:

for some reason, the array buffer is bigger than the file itself

enter image description here enter image description here

UPDATE after response observer added: enter image description here

Lars Hendriks
  • 998
  • 5
  • 22

0 Answers0