Skip to content

Instantly share code, notes, and snippets.

@FirstWhack
Created September 6, 2018 20:46
Show Gist options
  • Save FirstWhack/ae311d7fdaf8866a1a47b9bfe8532c5a to your computer and use it in GitHub Desktop.
Save FirstWhack/ae311d7fdaf8866a1a47b9bfe8532c5a to your computer and use it in GitHub Desktop.
getDownload(url, body, params) {
this.query(url, body, params, 'GET').then(resp => {
resp.blob().then(blob => this.blobDownload(blob, resp));
});
}
blobDownload(blob, resp) {
const fileName = (resp.headers.get('Content-Disposition') || '').match(/filename="?(.+[^"])"?/)[1]; // matches `filename="a.b"` or `filename=a.b` as 'a.b'
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
const url = window.URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.download = fileName;
anchor.href = url;
anchor.style = 'display: none';
document.body.appendChild(anchor);
anchor.click();
setTimeout(() => {
window.URL.revokeObjectURL(url);
anchor.remove();
}, 500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment