【问题标题】:Can I send XLS/XLSX files using Express res.sendFile()?我可以使用 Express res.sendFile() 发送 XLS/XLSX 文件吗?
【发布时间】:2020-12-09 02:14:55
【问题描述】:

我已经发布了太多关于此的问题,但我一直碰壁,所以我一直在问。可以使用 Express'res.sendFile() 发送 XLS 或 XLSX 文件吗?

这是我正在使用的代码:

res.sendFile(
    path.join(__dirname, '../testing/'),
    `${fileName}`,
    (err, data) => {
      if (err) throw err;
    }
  );

尽管fileName 的值为filename.xlsx,但它从不使用XLSX 文件;它始终默认为index.html。我总是收到以下错误:

[Error: ENOENT: no such file or directory, stat '/Users/username/Documents/app/testing/index.html']

【问题讨论】:

    标签: express xlsx sendfile


    【解决方案1】:

    sendFile 既不知道也不关心您正在尝试发送 XLS/XLSX 文件。它只知道您正在尝试发送文件。

    所以,是的,你可以,但你必须正确地打电话给sendFile。 ?

    您已将文件名作为单独的参数(will be parsed as the options argument),而不是放在 path 参数中。

    res.sendFile(
        path.join(__dirname, '..', 'testing', fileName),
        (err, data) => {
          if (err) throw err;
        }
      );
    

    请注意,我冒昧地删除了明显多余的表达式模板文字内容。我还为您拆分了'../testing/':为了清楚起见,您应该尽可能将分立组件放入path.join

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 2022-12-17
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多