从axios post api下载Zip文件,获得无效文件(React)



我可以下载文件但不能解压

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);
}
};
我不知道我做错了什么?任何帮助都是感激的

试试这个:

.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();
});

最新更新