Last active
October 6, 2020 06:02
-
-
Save NayabSiddiqui/bbfcaa6f1c90da9760af7a383bb42e2b to your computer and use it in GitHub Desktop.
Axios POST example to illustrate downloading of PDF from a REST api. Answer to the Stackoverflow question [Download binary file with Axios](https://stackoverflow.com/questions/49040247/download-binary-file-with-axios)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
axios.post("http://localhost:8080/reports/proposal-summary/", | |
payload, | |
{ | |
responseType: 'arraybuffer', | |
headers: { | |
'Content-Type': 'application/json', | |
'Accept': 'application/pdf' | |
} | |
}) | |
.then((response) => { | |
const url = window.URL.createObjectURL(new Blob([response.data])); | |
const link = document.createElement('a'); | |
link.href = url; | |
link.setAttribute('download', 'file.pdf'); //or any other extension | |
document.body.appendChild(link); | |
link.click(); | |
}) | |
.catch((error) => console.log(error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answer to the Stackoverflow question Download binary file with Axios.