Skip to content

Instantly share code, notes, and snippets.

@RobinHerbots
Created December 11, 2019 11:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save RobinHerbots/940b0fce3248b4deb1de7a6e4290635b to your computer and use it in GitHub Desktop.
Ajax file download
$('#GetFile').on('click', function () {
$.ajax({
url: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/172905/test.pdf',
method: 'GET',
xhrFields: {
responseType: 'blob'
},
success: function (data) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(data);
a.href = url;
a.download = 'myfile.pdf';
document.body.append(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment