1、
// 导出接口
export function downloadCollectList (params) {
return request({
url: '/apiUrl/api/ex/award/collect/export/awards',
method: 'get',
params: params,
responseType: 'blob' //后台返回的数据会被强制转为blob类型
})
} // config/detail
2、请求里面添加一个超时白名单,防止下载文件太大造成的超时
// 超时白名单
const timeoutUrl = [
"/apiUrl/api/eidos_mining/profit_issue_record/issue_profit",
"/apiUrl/api/pool/fomo/account/export",
"/apiUrl/api/ex/award/collect/export/awards"
];
if (timeoutUrl.includes(config.url)) {
config.timeout = 30000;
} (service.interceptors.request里面)
3、//方法
async exportList () {
this.$prompt('请设置下载的文件名', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
inputValue: new Date().getTime() //获取当前时间作为默认文件名,可修改
})
.then(async ({ value }) => {
const params = Object.assign({}, this.params)
const content = await redeem.downloadCollectList(params)
console.log("11",content) //如果这儿打印为underfined就看接口里面 的responseType: 'blob'是否添加
const blob = new Blob([content])
console.log("22",blob)
const fileName = value + '.xlsx'
if ('download' in document.createElement('a')) {
// 非IE下载
const elink = document.createElement('a')
elink.download = fileName
elink.style.display = 'none'
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else {
// IE10+下载
navigator.msSaveBlob(blob, fileName)
}
this.$message({
type: 'success',
message: '导出成功!'
})
})
.catch(error1 => {
console.log(error1)
this.$message({
type: 'info',
message: '导出已取消'
})
})
},
借鉴链接:https://blog.csdn.net/weixin_42142057/article/details/97655591