下载excel(接收文件流)

/**
 * 文件流转换 主要代码块,可自定义下载文件名称
 * @param {} data
 */
export function download(data, titName) {
  if (!data) {
    return
  }
  const content = data
  const blob = new Blob([content], { type: 'application/vnd.ms-excel' })
  const fileName = titName || 'EXCEl.xls'
  if ('download' in document.createElement('a')) { // 非IE下载
    const elink = document.createElement('a')
    elink.download = fileName
    elink.style.display = 'none'
    elink.href = URL.createObjectURL(blob)
    document.body.appendChild(elink)
    elink.click()
    URL.revokeObjectURL(elink.href) // 释放URL 对象
    document.body.removeChild(elink)
  } else { // IE10+下载
    navigator.msSaveBlob(blob, fileName)
  }
}
 
调用
      this.$axiosDownload('/purchaseCtrl/basicExcelExport', newData).then(result => {
        download(result, 'Basic-Price.xls') //result 为文件流
      })
posted @ 2021-04-25 11:47  吃不棒的胖胖糖  阅读(138)  评论(0编辑  收藏  举报