需求:前后端完全分离的项目,需要一个导出功能,前端使用ant design vue,后端 NetCore5.0,后台返回数据为 IActionResult
exportExcel(){
exportExcel().then((res)=>{
this.downloadfile(res)
// eslint-disable-next-line handle-callback-err
}).catch((err) => {
this.$message.error('下载错误:获取文件流错误')
})
},
downloadfile(res) {
var blob = new Blob([res.data], {
type: 'application/octet-stream;charset=UTF-8'
})
var contentDisposition = res.headers['content-disposition']
var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
var result = patt.exec(contentDisposition)
var filename = result[1]
var downloadElement = document.createElement('a')
var href = window.URL.createObjectURL(blob) // 创建下载的链接
var reg = /^["](.*)["]$/g
downloadElement.style.display = 'none'
downloadElement.href = href
downloadElement.download = decodeURI(filename.replace(reg, '$1')) // 下载后文件名
document.body.appendChild(downloadElement)
downloadElement.click() // 点击下载
document.body.removeChild(downloadElement) // 下载完成移除元素
window.URL.revokeObjectURL(href)
},