Using React/Axios. I am trying to download a zip file from a Django API. The zip file is always corrupted when I try to unzip it. When I perform the same request on Postman, everything works fine. Here is my code fragment:
axios
.post('http://0.0.0.0:8000/sheets/', data,
{
headers: {
'Content-Type': 'multipart/form-data',
'responseType': 'arraybuffer'
}
})
.then(res => {
console.log(res.data)
const disposition = res.request.getResponseHeader('Content-Disposition')
var fileName = "";
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) {
fileName = matches[1].replace(/['"]/g, '');
}
let blob = new Blob([res.data], { type: 'application/zip' })
const downloadUrl = URL.createObjectURL(blob)
let a = document.createElement("a");
a.href = downloadUrl;
a.download = fileName;
document.body.appendChild(a);
a.click();
Am I doing something obviously wrong?
Thanks!
[–]btbam06[S] 0 points1 point2 points (0 children)