**方法一**
//获取文件流,将文件流转为文件通过<a></a>导出,中间可以做处理
let url = baseUrl + '接口地址' + token
axios.get(url,{responseType: 'blob',timeout:600000}).then(async res => {
if (res.data.size < 100) {
let fileReader = new FileReader();
fileReader.readAsText(res.data);
let jsonData = await new Promise((resolve, reject) => {
fileReader.onload = function (result) {
resolve(JSON.parse(result.target.result))
}
})
if (!jsonData.success) {
this.$message.error(jsonData.description)
this.downloadLoading = false
return
}
}
let fileUrl = window.URL.createObjectURL(new Blob([res.data]))
let link = document.createElement("a")
link.href = fileUrl
link.style.display = 'none';
link.setAttribute('download', '文件.zip')
document.body.appendChild(link)
link.click()
window.URL.revokeObjectURL(fileUrl); // 释放URL 对象
document.body.removeChild(link);
fileUrl = null //便于垃圾回收
if (this.downloadLoading) {
this.downloadLoading = false
}
})
**方法二**
直接导出
params = {
url: baseUrl + '接口地址?token=' + token,
fileName: '',
}
download(params)
function download(params) {
if ('download' in document.createElement("a")) {
let link = document.createElement("a");
link.style.display = 'none';
link.href = params.url;
link.target = '_blank'
params.fileName && (link.download = params.fileName);
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(link.href); // 释放URL 对象
document.body.removeChild(link);
}
}
标签:常用,let,URL,link,download,params,document,方法,下载
From: https://www.cnblogs.com/LiZiheng/p/18330737