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('服务器链接错误!');
  });
相关文章
相关标签/搜索