项目中有一个导出列表的操作,由后端提供接口。这个也是当时百度下来的,在此记录一下。
接口需要设置responseType为 blob
没有设置类型时,结果是这样的
设置类型后
点击事件调用接口
async exportOut () {
const res = await api.dataApiExport({
appInfoId:this.$store.state.common.appId || sessionStorage.getItem('appId'),
tenantId:this.loginCtx.tenantId
})
let time = new Date()
let YY = time.getFullYear()
let MM = time.getMonth()+1
let DD = time.getDate()
let blob = new Blob([res], { type: "application/vnd.ms-excel" })
let objectUrl = URL.createObjectURL(blob)
let link = document.createElement('a')
link.style.display = 'none'
link.href = objectUrl
link.setAttribute('download', `API列表_${YY+'-'+MM+'-'+DD}.xls`)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
},