I have implemented a backend where administrators can create backups. These backups are collected in zip files and are stored in a non-public folder on the server.
The administrators should be able to download the zip files afterwards. But this does not work properly. The file is downloaded, but when you try to unzip it, you get the message "An attempt was made to move the file pointer before the beginning of the file". The zip file does not seem to be the problem. If you unpack it on the server, the unpacking works without errors. There must be something wrong with the download process.
const downloadHandler = (params) => {
axios.get(`/backup/download?name=${params.filename}`).then((res) => {
const downloadUrl = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = downloadUrl;
link.setAttribute('download', 'file.zip');
document.body.appendChild(link);
link.click();
link.remove();
});
};
The downloadHandler shown above is called in the React app as soon as a user clicks on the download button of a backup. After that the download menu of the browser opens for direct download.
Server implementation:
router.route('/backup/download').get((req, res, next) => {
try {
res.download(path.join(backupPath, req.query.name));
} catch (err) {
next(err);
}
});
I have already spent hours looking for a solution, but no matter what I try, it doesn't work. The problem is, as I said, that the file cannot be unzipped after downloading.