util.exportUrl=function(url,mthod,data){
    const a = document.createElement('a')
    // 使用fetch方法获取response.headers响应头
    fetch(url, {
      method: mthod,
      body: JSON.stringify(data),
      headers: {
        token: localStorage.getItem('token'),
        'Content-Type':'application/json;charset=UTF-8'
      },
    }).then(res => {
      // 切割出文件名
      const fileNameEncode = res.headers.get('content-disposition').split(';')[1].split('filename=')[1]
      // 解码
      const fileName = decodeURIComponent(fileNameEncode)
      console.log('fileName', fileName)
      // 使用blob()方法获取blob对象数据
      res.blob().then(
        res => {
          // 设置type类型
          const blob = new Blob([res], {
            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; application/octet-stream'
          })
          const fileUrl = window.URL.createObjectURL(blob)
          a.href = fileUrl
          a.setAttribute('download', fileName)
          a.style.display = 'none'
          a.click()
          a.remove()
        }
      )
    })
},

 

posted on 2023-01-11 15:59  鄢宁  阅读(145)  评论(0编辑  收藏  举报