使用Blob,下载文件流。

js部分

// 导出
    exportCodeFn() {
      notActiveExport().then((res) => {
        let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
        let fileName = '文件.xlsx'
        if (window.navigator.msSaveOrOpenBlob) {
          navigator.msSaveBlob(blob, fileName)
        } else {
          var link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          link.download = fileName
          link.click()
          //释放内存
          window.URL.revokeObjectURL(link.href)
        }
      })
    },

接口部分

import { getfile} from '@/http/request'
import { HOST} from '@/http/config'
// 导出
export function notActiveExport(params) {
  return getfile(`${HOST}aa/test`, params)
}

配置getfile接口部分 request.js

import axios from 'axios'
import router from '@/router'
// 下载文件的接口
export function getfile(url, params = {}) {
  let token = localStorage.getItem("token")
  return axios.get(url, {
    params,
    headers: {
      'Authentication': token,
    }, responseType: 'blob'
  })
}

猜你喜欢

转载自blog.csdn.net/weixin_44948981/article/details/128846795