Home > Back-end >  How do I handle res.download(pathname) on frontend(ReactJS)
How do I handle res.download(pathname) on frontend(ReactJS)

Time:05-14

I have written a code on my server(NodeJS) that will send a .csv file to the frontend with express res.download(path). I would like to know how I can handle the response with fetch API that will allow the frontend to download the file(.csv) sent.

I am using reactJs on my frontend.

CodePudding user response:

I was able to solve this by using the downloadJs on my frontend

import download from 'downloadjs'
...
//get request
fetch(url).then(res => res.blob())
.then(blob => download(blob, 'name of file', 'data type')) // this line automatically starts a download operation
.catch(err => console.log(err));

I hope this helps you

  • Related