導出---后台返回二進制流文件數據,前端轉換格式進行下載


isExcel(type,name, data) {
  const link = document.createElement('a')
  const blob = new Blob([data])
  link.style.display = 'none'
  link.href = URL.createObjectURL(blob)
  link.setAttribute('download', `${name}.`+type)
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
},
fuDown() {
      let parmas = {
        fileName: this.blogData.annexUrl.split("/")[
          this.blogData.annexUrl.split("/").length - 1
        ],
      };

      downloadFu(parmas)//集成的axios
        .then((res) => {
          console.log("下載");
          console.log(res);
          console.log("res");
          this.$tool.isExcel(this.type, this.typeName, res);
        })
        .catch((res) => {
          console.log(res.msg);
          console.log("res.msg");
        });

    //原生axios//
// axios // .get("http://172.16.9.224:8090/blogs/blog/download", { // params: { // fileName: this.blogData.annexUrl.split("/")[ this.blogData.annexUrl.split("/").length - 1 ], //請求參數,文件名稱 // }, // headers: { // 'X-Token': '1635c0cbc5f64e63b4d8f2bb171db48d#ACCOUNT#89177' // }, // responseType: 'blob', //二進制流 ,請求時需要配置響應類型 // }) // .then((res) => { // console.log("數據是:", res); // this.$tool.isExcel(this.type,this.typeName,res.data) type文件類型、typename 文件名稱 data返回的二進制流數據

// }) // .catch((e) => { // console.log("獲取數據失敗"); // }); },

 補充方法:

export function buildExport(name, url, params = {}) {
  console.log("傳入的參數-------------",params,(process.env.VUE_APP_API_URL?process.env.VUE_APP_API_URL:configs.baseURL.target) + url)
  axios({
    method: 'post',
    url: (process.env.VUE_APP_API_URL?process.env.VUE_APP_API_URL:configs.baseURL.target) + url,
    data:  {param:encryptList.Encrypt(JSON.stringify(params))},
    responseType: 'blob',
    headers: { 'token': tool.getToken(),appId:tool.getStorage("appId") }
  }).then(res => {
    const link = document.createElement('a')
    const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
    link.style.display = 'none'
    link.href = URL.createObjectURL(blob)
    link.setAttribute('download', `${name}.xls`)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link);
  }).catch(err => {
    console.log('導出報錯', err);
  });
}

 


免責聲明!

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



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