美文网首页
导出文件

导出文件

作者: zkzhengmeng | 来源:发表于2024-11-17 15:20 被阅读0次
    <div @click="exportLpb" ></div> 
      exportFile(){
        let baseUrl = 'http://localhost:8085/#/buildingManage'
        let loading = this.$globalLoading() ;  //自己项目封装,根据个人情况使用
        let fileName = ''
        fetch(baseUrl , {
          method: 'GET',
          headers: {
            'Content-Type': 'application/octet-stream'
          }
        }).then(response =>{
            fileName = response.headers.get('Content-Disposition')
            return response.blob()
          })  // 将响应转换为 Blob
          .then(blob => {
            loading.close()
            // 创建一个指向 Blob 的 URL
            const url = window.URL.createObjectURL(blob)
            const a = document.createElement('a')
            a.style.display = 'none'
            a.href = url 

            // 设置下载文件名
            a.download = decodeURIComponent(fileName.substring(fileName.indexOf('filename=') + 'filename='.length));  // 可根据实际情况设置文件名

            // 触发下载
            document.body.appendChild(a)
            a.click()

            // 清理 URL 和 DOM
            window.URL.revokeObjectURL(url)
            document.body.removeChild(a)
          })
          .catch(error => {
            loading.close();
            console.error('文件下载失败:', error)
          })
      },

相关文章

网友评论

      本文标题:导出文件

      本文链接:https://www.haomeiwen.com/subject/piwujjtx.html