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

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

例如:json

  this.$axios({axios

    method: 'post',服務器

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

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

    url: url,this

    data: dataurl

  })orm

  .then( res =>{xml

    //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('服務器鏈接錯誤!');
  });
相關文章
相關標籤/搜索