#StackBounty: #reactjs #spring #file-saver Cannot open downloaded zip file from rest endpoint using file-saver

Bounty: 50

Here is the backend code for the download endpoint:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(byteArrayOutputStream);
for (Long id : ids) {
    // Get the "generated" file using the id    
    zipOut.putNextEntry(new ZipEntry(generated.getName() + ".doc"));
    InputStream inputStream = new ByteArrayInputStream(generated.getFile().getBytes(1, (int)generated.getFile().length()));
    IOUtils.copy(inputStream, zipOut);
    zipOut.closeEntry();
}
zipOut.close();

response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=File.zip");

byte[] zipBytes = byteArrayOutputStream.toByteArray();
OutputStream outputStream = response.getOutputStream();
outputStream.write(zipBytes);
outputStream.close();
response.flushBuffer();

And for the frontend, I am using axios and file-saver

import { saveAs } from "file-saver";

request.then((response: any) => {
  const blob = new Blob([response.data], { type: "application/zip" });
  saveAs(blob, "Report.zip");
});

I can download the zip file, but when I tried to open, I got the follwing error:

"An attempt was made to move the file pointer before the beginning of the file"


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.