vue+axios excle文件导出

2019-09-30  本文已影响0人  妖刀_

res 为接口返回数据   当获取excle文件时  接口请求头配置

axios.responseType ='blob'

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

const blob =new Blob([res.data])          // 将返回二进制数据  创建Blob文件

const fileName =`discount-code-${Date.parse(new Date())}.xls`  // 导出文件名 + 时间毫秒值 避免名称重复

if ('download' in document.createElement('a')) {

const elink = document.createElement('a')  创建 a标签

elink.download = fileName  //  创建文件名

elink.style.display ='none'  //   导出文件样式

  elink.href = URL.createObjectURL(blob)  创建a 标签 href 

document.body.appendChild(elink)  

elink.click()

URL.revokeObjectURL(elink.href)  

document.body.removeChild(elink)  释放流文件

}

上一篇下一篇

猜你喜欢

热点阅读