接口配置 :
需要加上 { responseType: 'blob' }
import Vue from 'vue';
export default {
// 导出
exportxls: (params) => {
return Vue.axios.post('/das-sesp-data/api/company-qualities/excel', params, { responseType: 'blob' })
}
}
导出方法配置:
import api from '../../api/api-per-certificate';
// 导出选中或者全部内容
importPlanTask() {
this.$prompt('请输入文件名', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消'
}).then(({ value }) => {
let fileName = ''
if (value.search('.xlsx') === -1 && value.search('.xls') === -1) {
fileName = value + '.xlsx'
}
let excelcs = {
// 导出的文件id组 api.exportxls
ids: this.multipleSelection
}
// 调用导出文件接口
api.exportxls(excelcs).then((res) => {
if (!res) {
this.$message.error('下载失败,解析数据为空!')
return
}
// 创建一个新的url,此url指向新建的Blob对象
const url = window.URL.createObjectURL(new Blob([res], { type: 'application/vnd.ms-excel' }))
// 创建a标签,并隐藏改a标签
const link = document.createElement('a')
link.style.display = 'none'
// a标签的href属性指定下载链接
link.href = url
// setAttribute() 方法添加指定的属性,并为其赋指定的值。
link.setAttribute('download', fileName)
document.body.appendChild(link)
link.click()
link.remove()
this.$Message.info('导出成功')
})
})
},