两种方法,大差不差
blob方法:
axios.get / post('/---',{responseType:'blob'}).then(res => {
let tagA = document.createElement('a')
let blob = new Blob(['返回值'], {
type: "application/md.ms-excel"
});
let objectUrl = URL.createObjectURL(blob)
tagA.setAttribute('href', objectUrl)
tagA.setAttribute('download', '导出的名称。xls')
tagA.click()
})
a标签导出
axios.post(
`https://______________`,
{},
{
responseType: "blob",
}
)
.then((res) => {
let tagA = document.createElement("a");
tagA.innerHTML = "下载";
tagA.style.display = "none";
const url = URL.createObjectURL("路径");
tagA.href = url;
document.body.append(tagA);
tagA.click();
window.URL.revokeObjectURL(url);
})
.catch((err) => {
console.log(err);
});