小编res*_*yte的帖子

NodeJS 客户端(React)如何下载 excel 文件?

我有两个独立的项目。客户端:React,服务器:NodeJS

我通过 React 提交表单来在 NodeJS 端创建 excel。

我想从 React 端下载这个 excel。但是,我无法下载它。

NodeJS 代码

  res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
  workbook.xlsx.write(res)
      .then(function(){
          res.end();
      });
Run Code Online (Sandbox Code Playgroud)

NodeJS返回网络层

在此输入图像描述

首先尝试 React.js 代码

 startDownload(response,"resobyte.xlsx")

function startDownload(file, fileName) {
    const url = window.URL.createObjectURL(new Blob([file]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', fileName);
    document.body.appendChild(link);
    link.click();
    link.parentNode.removeChild(link);
}
Run Code Online (Sandbox Code Playgroud)

第二次尝试 React.js 代码

  let blob = new Blob([response], {type: 'vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
            FileSaver.saveAs(blob, 'fileName.xlsx");
Run Code Online (Sandbox Code Playgroud)

文件已下载。

我的反应服务调用

export const createExcel = async (model) => {
  let response = () => {
      return …
Run Code Online (Sandbox Code Playgroud)

node.js reactjs exceljs

5
推荐指数
0
解决办法
1772
查看次数

标签 统计

exceljs ×1

node.js ×1

reactjs ×1