后端返回二进制流文件 excle导出 或者xls xlsx docx pdf导出

方法一 

//数据查询记录 excle导出
export function exportInterfaceSearch(data) {
  return request({
    url: '/risen/data/exportInh',
    method: 'post',
    responseType: 'blob',   // 第一步
    data
  })




第二步  res 是后端返回的二进制流文件

        const url = window.URL.createObjectURL(res)
        const a = document.createElement('a')
        a.style.display = 'none'
        a.href = url
        a.download = '数据目录'
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
        window.URL.revokeObjectURL(url)

或者 这种写法

//接口列表导出
export function exportInterfaceManagement(params) {
  return request({
    method: 'get',
    isFormData: true,
    url: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    responseType: 'blob',
    params
  })
}
    async impotout() {//导出
    
      exportInterfaceManagement({
   
      }).then((res) => {
        const blob = new Blob([res], {
          type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        })
        const a = document.createElement('a')
        const href = window.URL.createObjectURL(blob)
        a.href = href
        a.download = '接口列表信息'
        document.body.appendChild(a)
        a.click()
        // console.log(a)
        document.body.removeChild(a)
        window.URL.revokeObjectURL(href)
        // console.log(res)
      })

方法二 ://多种类型 可以定义一个方法 导出

api

//接口管理文档下载
export function exportwordlist(params) {
  return request({
    method: 'get',
    isFormData: true,
    url: '/sen/gatay/interce/downInteceDoc',
    responseType: 'blob',
    params
  })
}

定义文档 名称 和类型 

  exportwordlist({interfaceUuid: record.uuid}).then(res => {
            this.$_downloadFile(res, '接口文档', 'doc') //res:返回数据
  })
this.$_downloadFile(res, '接口文档', 'doc') //res:返回数据

 

  $_downloadFile(obj, name, suffix) {
      const DOWNLOAD_TYPE_MAP = {
        xls: 'application/vnd.ms-excel',
        xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        doc: 'application/msword',
        docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        pdf: 'application/pdf'
      }
      if (!DOWNLOAD_TYPE_MAP[suffix]) {
        throw new Error('请传入文件下载的格式后缀,eg:xls,xlsx,doc,docx,pdf')
      }
      const blob = new Blob([obj], {
        type: DOWNLOAD_TYPE_MAP[suffix]
      })
      const fileName = `${name}.${suffix}`
      let link = document.createElement('a')
      document.body.appendChild(link)
      link.href = URL.createObjectURL(blob)
      link.setAttribute('download', fileName)
      link.click()
      document.body.removeChild(link)
      URL.revokeObjectURL(link.href) // 销毁url对象
      this.$message.success('下载成功')
    },

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值