react用fetch实现导出Excel表

// 由前端定义文件名及文件类型
exportByProductNo = (item) => {
    const reqUrl = `/api/configure/exportConfigure?productKey=${item.productNo}`;
    fetch(reqUrl).then(resp =>resp.blob())
      .then(blob => {
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = "filename.xls";
        a.click();
      })
  };

// 由服务器返回文件名及文件类型
  exportByProductNo = (item) => {
    const reqUrl = `/api/configure/exportConfigure?productKey=${item.productNo}`;
    fetch(reqUrl).then(resp => resp.blob().then(blob => {
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      const fileName = resp.headers.get('Content-Disposition').split('=')[1];
      a.download = decodeURIComponent(fileName);
      a.click();
    }))
  };

猜你喜欢

转载自blog.csdn.net/Tanganq/article/details/88891644