React Dva根据后台返回的的数据前端下载Exl表格

代码:

//点击导出按钮

const handleClickExport=async()=>{

if(data.length==0){

message.error("请选择将要导出的门店")

}else{

const res = await axios.post(storeExport, {//storeExport 为后台的数据接口

appKey:'appkey',

data: {

ids: data,

properties:["name","address","serveTime","orderTotal","appointmentOrderCount"],

titles:[ "门店名","地址","服务时间","订单总数","预约订单数" ],

query:{

name:nameSearch,

address:''

}

},

version: '1.0'

//这是后台要求的请求体格式,按照接口的要求

}, {

responseType: 'blob',

onDownloadProgress: e => {

}

});

download(res.data, '门店列表.xls');

}

download的方法:

//下载文件

export function download(file, fileName) {

  let userAgent = navigator.userAgent;

  const url = window.URL.createObjectURL(file);

  const a = document.createElement('a');

  a.href = url;

  a.download = fileName || 'download';

  a.target = '_blank';

  if (userAgent.indexOf("Firefox") > -1) {

    var evt = document.createEvent("MouseEvents");

    evt.initEvent("click", true, true);

    a.dispatchEvent(evt);

  }else{

    a.click();

  }

}

猜你喜欢

转载自blog.csdn.net/Zeng__Yi/article/details/83268792