若后端为post传参,且返回的是文件流,那么可以用如下第一种方法:
export() {
let cookie = localStorage.xxx();
const params = {
xxxx: xxxxxx,
xxxx: xxxxxx,
cookie,
};
fetch(
"http://api.xxx.xxxxx.cn/xxxxx/?post=json",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(params),
responseType: "blob",
}
)
.then((res) => res.blob())
.then((blob) => {
const a = document.createElement("a");
a.style.display = "none";
a.href = URL.createObjectURL(blob);
a.download = "数据详情";
document.body.appendChild(a);
a.click();
// 移除元素
document.body.removeChild(a);
});
},
若后端返回的是文件地址,便可以拼接参数直接访问,可使用如下第二种方法。
export() {
let cookie = localStorage.xxx();
const params = {
xxxx: xxxxxx,
xxxx: xxxxxx,
cookie,
};
window.location.href =
process.env.ROOT_API +"/xxxx/xxxxxx/" +this.paramToStr(params);
},