Home > Enterprise >  Downloading Zip file from axios post api getting invalid file (React)
Downloading Zip file from axios post api getting invalid file (React)

Time:05-17

i can download file but invalid on extract

  const handleDownloadPicture = async (ids: number) => {
  try {
  const response = await axios
    .post(`/api/folders/${folderDetails.id}/download`, {
      resources: ids,
    })
    .then((res) => new Blob([res.data]))
    .then((blob) => {
      const link = document.createElement('a');
      const url = URL.createObjectURL(blob);
      console.log(url);
      link.href = url;
      link.download = 'myfile.zip';
      link.click();
    });
} catch (error) {
  console.error(error);
}
};

i don't know what i'm doing wrong? any help is appreciated

CodePudding user response:

Try this:

  .post(`/api/folders/${folderDetails.id}/download`, {
      resources: ids
    }, {responseType: 'blob'})
    .then(({data: blob}) => {
      const link = document.createElement('a');
      const url = URL.createObjectURL(blob);
      console.log(url);
      link.href = url;
      link.download = 'myfile.zip';
      link.click();
    });
  • Related