1

If I type the url into my browser bar and press enter the file downloads successfully. But if I call the same url in my React app using Axios, the file does not download. This is my backend code:

router.get('/', async  function(req, res) {

     var a = process.cwd()+ '/platforma343/public/uploads/studentAccounts/studentAccount_2021_22.csv';
     const fileName = req.params.name;

  res.download(a , "fileName", (err) => {
    if (err) {
      res.status(500).send({
        message: "Could not download the file. " + err,
      });
    }
  });
    
});
router.get('/', async  function(req, res) {

     var a = process.cwd()+ '/platforma343/public/uploads/studentAccounts/studentAccount_2021_22.csv';
     const fileName = req.params.name;

  res.download(a , "fileName", (err) => {
    if (err) {
      res.status(500).send({
        message: "Could not download the file. " + err,
      });
    }
  });
    
});

and this is the frontend code :

  handleDownload = (event) => {
    event.preventDefault();

      axios({
        url: 'http://localhost:9000/downloadFile?filename=1',
        method: 'GET',
        responseType: 'blob', // important
      }).then((response) => {
        console.log(response);
      });
      
  };
apanay22
  • 567
  • 1
  • 5
  • 10

0 Answers0