0

I've posted too many questions about this but I keep hitting a wall so I keep asking. Can you send an XLS or XLSX file using Express' res.sendFile()?

Here's the code I'm using:

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

Even though fileName has the value of filename.xlsx, it never uses the XLSX file; it always defaults to index.html. I always get the following error:

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

1 Answers1

0

sendFile neither knows nor cares that you're trying to send an XLS/XLSX file. It only knows that you're trying to send a file.

So, yes, you can, but you have to call sendFile properly.

You've put the filename as a separate argument (which will be parsed as the options argument), instead of putting it in the path argument.

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

Notice that I took the liberty of removing the apparently redundant expression template literal stuff. I've also split '../testing/' up for you: you're supposed to put discrete components into path.join as much as possible, for clarity.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35