Skip to content

Instantly share code, notes, and snippets.

@tauqeernasir
Created September 8, 2020 07:49
Show Gist options
  • Save tauqeernasir/af8cb3e414a19b4a7a9271149b81aac4 to your computer and use it in GitHub Desktop.
Save tauqeernasir/af8cb3e414a19b4a7a9271149b81aac4 to your computer and use it in GitHub Desktop.
Downloading file with AJAX request in browser
// ANY AJAX LIBARARY CAN BE USED
// `responseType: blob` is important
axios({
url: 'http://localhost:9000/url/to/example.pdf',
method: 'POST',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
document.body.appendChild(link);
link.click();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment