Skip to content Skip to sidebar Skip to footer

How To Download A File Through An Api In React?

In my react app, I have a component which has a file download button for download a file coming from the Back end. I'm using AXIOS for the AJAX call. The problem is, after download

Solution 1:

As I am not able to add comments so posting as answer. I have tried the same thing and posted the question for same in this link.

For post method i get the success with fetch as below.

fetch("url",
        { 
            method: "POST",
            headers: { "Content-Type": "application/json",'Authorization': 'Bearer ' + window.localStorage["Access_Token"]},
            body:data
        }).then(response => response.blob()).then(response => ...*your code for download*... )

You are getting corrupted file because you are not receiving content as blob or arraybuffer.

Post a Comment for "How To Download A File Through An Api In React?"