I created a GET API that downloads a file when called. However, when I use axios to make the call, the file does not download. I found that axios is returning response:data blob format. How can I download the file?
API call using axios:
export const downloadFile = (fileId) => apiClient.get(`/download/${fileId}`)
React code:
function callDownloadFileApi(){
downloadFile('f5aa890c-8085-4209-8c0c-ee8522d7fb8c')
.then(response => console.log(response))
.catch(error => console.log(error))
}
2
Answers
A common solution is using this library – https://www.npmjs.com/package/react-native-fs
❌ There is a point with this question ❌,
When you want to download sth, it means that you want to get a file from the server and save it inside your local system (For example on your mobile phone), In this case calling an API to start downloading doesn’t make sense 🧐.
You need to first get the file path from the server, and then download it with the help of the react-native-fs as @Fiston Emmanuel said.