Skip to content

Instantly share code, notes, and snippets.

@RobinHerbots
Created December 11, 2019 11:23
Show Gist options
  • Save RobinHerbots/940b0fce3248b4deb1de7a6e4290635b to your computer and use it in GitHub Desktop.
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