随笔 - 112,  文章 - 0,  评论 - 2,  阅读 - 93932
复制代码
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   鄢宁  阅读(176)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示