0

At backend, I have the following NodeJS script:

app.get('/getDownloadTemplate', cors(), (req, res) => {
    res.sendFile(path.join(__dirname, '../temp.xlsx'));
});

At frontend, I have React code like this:

axios.get('http://127.0.0.1:3000/getDownloadTemplate').then((response) => {
    return response;
});

When I manually paste http://127.0.0.1:3000/getDownloadTemplate on the chrome, the file temp.xlsx is automatically downloaded to my Downloads folder.

But when I try to download via React, nothing happens.

Can anyone help me?

Dimisizz
  • 69
  • 9
  • Take a look at your browser's debug console: see if there isn't any warning there; take a look, also, at the network tab to see if there is something there. – Haroldo_OK Nov 16 '20 at 10:00
  • 1
    I tried to do console.log('debugging') above the return response line. When I click a button, I can see the word 'debugging' in the debug console. At the network tab, it goes to getDownloadTemplate with status 200. – Dimisizz Nov 16 '20 at 10:07
  • 1
    If you check tour network tab, you’ll probably see that your file has been downloaded into you browser’s. You need to add more javascript to download it “outside” of your browser: https://stackoverflow.com/a/54626214/10333882 – Hugh Nov 16 '20 at 10:23

0 Answers0