I have a CreatePdf function on my Api an it returns a pdf file.When ı click a download button on a react page, it send a request to this function and trigger it.I can see the response like this image.But I could not manage to download it when I click the button. Also if i open this response on a new blank page, it automatically download the pdf.But I can not download it with button.
Asked
Active
Viewed 340 times
1
-
This can help you. Check this link : https://stackoverflow.com/questions/50694881/how-to-download-file-in-react-js – F4RZ1N Aug 10 '22 at 09:26
2 Answers
1
You can use anchor tag with download option..
<Button>
<a href={"download pdf link"} download="PdfFileName">
Download
</a>
</Button>

sms
- 1,380
- 1
- 3
- 5
0
const getPdfDownload = () => {
const url = "YOUR_URL";
axios
.get(url, {
responseType: "arraybuffer",
headers: {
Authorization: sessionStorage.getItem("AUTH_TOKEN") || "",
"Content-Type": "application/json",
Accept: "application/pdf",
},
})
.then((response:any) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'FILENAME.pdf');
document.body.appendChild(link);
link.click();
}).catch((err)=>{
console.log("Error", err);
})
};

IAMNITESHPANDIT
- 61
- 1
- 1
- 5
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 06 '23 at 07:14