jsZip creates empty zip folder

59 views Asked by At

When I download zip file, it size is corrent but it is empty.

Backend code (controller):

exports.downloadPreparedReport = catchAsync(async (req, res) => {
  const reportId = req.params.id;
  const filePath = `${process.cwd()}/reports/${reportId}`;
  if (fs.existsSync(filePath)) {
    const file = fs.readFileSync(filePath);
    zip.file(filePath, file);
    zip.generateAsync({ type: 'nodebuffer' }).then((content) => {
      res.end(content);
    });
  } else {
    await pool.query('DELETE FROM reports WHERE id = $1', [path.basename(filePath, path.extname(filePath))]);
    throw 'No file found, deleting this data from database.';
  }
});

Frontend code (request part):

  const res = await axios.get(`/report-service/download/${id}.${extType}`, {
    responseType: 'arraybuffer',
  });

Frontend code (download part - file is response coming from axios and fileName e.g file.zip)

const downloadLinkGenerator = (file: any, fileName: string) => {
  if (file) {
    const url = window.URL.createObjectURL(new Blob([file], { type: 'application/zip' }));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', fileName);
    document.body.appendChild(link);
    link.click();
    link.remove();
  }
};

This code downloads a zip file and it has size but it shows as empty folder. Where is the issue?

0

There are 0 answers