Skip to content

Instantly share code, notes, and snippets.

@DearVikki
Last active July 13, 2022 06:37
Show Gist options
  • Save DearVikki/12b0271fae4faad9ad377b9f49f781ec to your computer and use it in GitHub Desktop.
Save DearVikki/12b0271fae4faad9ad377b9f49f781ec to your computer and use it in GitHub Desktop.
Files
// this is another one
var request = new XMLHttpRequest();
request.open('POST', $someUrl, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.responseType = 'blob';
request.onload = function() {
// Only handle status code 200
if(request.status === 200) {
// Try to find out the filename from the content disposition `filename` value
var disposition = request.getResponseHeader('content-disposition');
var matches = /"([^"]*)"/.exec(disposition);
var filename = (matches != null && matches[1] ? matches[1] : 'file.pdf');
// The actual download
var blob = new Blob([request.response], { type: 'application/pdf' });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
// some error handling should be done here...
};
request.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment