vue使用axios下載excel表格(二進制)


axios設置返回格式為arraybuffer 或者 blob(最佳 不會亂碼)

例如:

  this.$axios({

    method: 'post',

    header: {'Content-Type': 'application/xls'},            // http請求類型

    responseType: 'blob',                      // 返回格式,默認json,可選arraybuffer、blob、document、json、text、stream

    url: url,

    data: data

  })

  .then( res =>{

    //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet這里表示xlsx類型

    let blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});

    let downloadElement = document.createElement('a');

            let href = window.URL.createObjectURL(blob);

            downloadElement.href = href;

    downloadElement.download = '反饋消息表.xls';          // xxx.xls/xxx.xlsx

            document.body.appendChild(downloadElement);

            downloadElement.click();

            document.body.removeChild(downloadElement);

            window.URL.revokeObjectURL(href);

  })

  .catch( err => {
    this.$message.error('服務器連接錯誤!');
  });


免責聲明!

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



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