vue實現文件下載功能


對接口進行請求:

//導出excel表到桌面
getData.exportexcel = (data)=>{ 
  return http({
    method: 'post',
    url: baseUrl + '/exportexcel/',
    data: {
        firstName: 'Fred',
        lastName: 'Flintstone'
    },
    responseType: 'blob'
  })
}

對請求回來的數據進行方法下載和調用:

      exportexcel(){
        //打印報表
        url.exportexcel().then(res=>{
          console.log(res,this,'----------數據')
          this.download(res)
        })
        console.log('---------點擊按鈕')
      },
      download (data) {
          if (!data) {
              return
          }
          let url = window.URL.createObjectURL(new Blob([data]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.id='Adownload'
          link.setAttribute('download', 'excel.xls') //命名可能會出現問題,格式一定和后端下載的格式一樣
          
          document.body.appendChild(link)
          link.click()
          document.getElementById('Adownload').remove();
      }

點擊下載按鈕進行下載操作

<div @click="exportexcel">左側屏幕</div>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM