当我收到@uFÑÉ:¤µÞ(ïÀ©zTB$ðÛYæÛLQ5ôUJñP⁄C.$Ì^ÖÔ#2A,é)ÊâðÜmÛ¡Æ'Þ±XRÀ⁄Q¶/éhOêOþyås<时,我想下载一个Zip文件
来 self 的 api 端点的此类数据。
这是我下载 zip 文件的代码,我将从 api 端点获取的数据存储到generateFile中:
{
this.state.generateFile ?
<Button onClick={() => {
const url = window.URL.createObjectURL(
new Blob([this.state.generateFile], { type: "application/zip" })
);
const link = document.createElement("a");
link.href = url;
link.setAttribute("saveas", "generate.zip");
document.body.appendChild(link);
link.click();
this.setState({ generateFile: null });
}} >Download</Button>
: null
}
最佳答案
使用 responseType: 'arraybuffer' 调用您的 axios
Axios.post('https://...',
{
"generation_date": this.state.generateDate,
"merchandiser": true, "superviser": true
},
{
responseType: 'arraybuffer', // add this line
headers: {
Authorization: "JWT " + localStorage.getItem("token")
}
}).then(res =>
this.setState({ generateFile: res.data }
))
关于javascript - ReactJS 中的 Zip 文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550826/