从 axios 下载 Zip 文件 post api 得到无效文件 (React)
Downloading Zip file from axios post api getting invalid file (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();
});
我可以下载文件但解压无效
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();
});