一、this.exportLoading = true
setTimeout(() => {
this.exportLoading = false
}, 3000)
let url = ‘’
if (process.env.NODE_ENV === ‘development’) {
url = https://.../downloadSpaceExcel?cityCode=${this.form.cityCode}&projectid=${this.form.projectid}&conditions=${this.form.conditions}&spaceType=${this.spaceTypeTurn(this.form.spaceType)}&status=${this.statusTurn(this.form.status)}¤t=1&pageSize=10000000
} else if (process.env.NODE_ENV === ‘test’) {
url = https://.../downloadSpaceExcel?cityCode=${this.form.cityCode}&projectid=${this.form.projectid}&conditions=${this.form.conditions}&spaceType=${this.spaceTypeTurn(this.form.spaceType)}&status=${this.statusTurn(this.form.status)}¤t=1&pageSize=10000000
} else {
url = https://.../downloadSpaceExcel?cityCode=${this.form.cityCode}&projectid=${this.form.projectid}&conditions=${this.form.conditions}&spaceType=${this.spaceTypeTurn(this.form.spaceType)}&status=${this.statusTurn(this.form.status)}¤t=1&pageSize=10000000
}
var xhr = new XMLHttpRequest() // 定义http请求对象
xhr.open(‘get’, url, true)
xhr.setRequestHeader(‘Content-type’, ‘application/x-www-form-urlencoded’)
xhr.setRequestHeader(‘token’, window.localStorage.getItem(‘token’))
xhr.send()
xhr.responseType = ‘blob’ // 返回类型blob
xhr.onload = function() { // 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
if (this.status === 200) {
var blob = this.response
console.log(xhr)
var reader = new FileReader()
reader.readAsDataURL(blob) // 转换为base64,可以直接放入a标签href
reader.onload = function(e) {
console.log(e) // 查看有没有接收到数据流
// 转换完成,创建一个a标签用于下载
var a = document.createElement(‘a’)
a.download = ‘公共空间表’ + dayjs().format(‘YYYY-MM-DD’) + ‘.xlsx’ // 自定义下载文件名称
a.href = e.target.result
a.click()
}
} else {
this.$message.error(‘导出失败’)
}
二、let params = {}
this.http.get(接口地址
, { responseType: ‘arraybuffer’, params }).then(res => {
const blob = new Blob([res], { type: ‘application/vnd.ms-excel’ })
const objectUrl = URL.createObjectURL(blob) // 创建URL
const link = document.createElement(‘a’)
link.href = objectUrl
link.download = ‘数据目录’ + dayjs().format(‘YYYY-MM-DD’) + ‘.xlsx’ // 自定义文件名
link.click() // 下载文件
URL.revokeObjectURL(objectUrl) // 释放内存
}).catch(() => {
this.$message({
type: ‘error’,
message: ‘下载失败…’
})
})