Skip to content Skip to sidebar Skip to footer

How To Download Files From Node Express Server?

I have a file on my server (Node.js + Express). I need to download file from server to computer of user. in React app I call the function which would download file: downloadFileF

Solution 1:

you have to install "js-file-download" library in react using following command

npm install --save js-file-download

then the code in react file as follows:

import download from'js-file-download';
downloadFile = () => {
   axios.get("localhost:3030/getfile")
     .then(resp => {
            download(resp.data, fileName);
     });
}

the code at server side is as follows:

router.get("/getfile", (req, res) => {
  FileLocation ="public/FILE.pdf"
  file="File.pdf"
 res.download(fileLocation, file, (err) => {
                if (err) console.log(err);
            });
});

My reference for this answer is in this link.

This works for me.I hope it works for you.

Post a Comment for "How To Download Files From Node Express Server?"