前端请求二进制流下载
- 设置请求返回类型 responseType: "arraybuffer",
- da.blob(); 返回二进制流 da.json() 返回json数据,此处使用bolb接收
- 创建下载地址 URL.createObjectURL(
new Blob([blob], { type: "application/vnd.ms-excel" })
);
- 设置下载名称 a.download =
下载名称
;
(fetch as any)("你的url", {
method: "post",
mode: "cors",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
responseType: "arraybuffer",
},
body: JSON.stringify({
startDate:"",
}),
})
.then((da: any) => {
return da.blob();
})
.then((blob: any) => {
let url = URL.createObjectURL(
new Blob([blob], { type: "application/vnd.ms-excel" })
);
let a = document.createElement("a");
a.target = "_banck";
a.href = url;
a.download = `下载名称`;
document.body.appendChild(a);
a.click();
});
```