Created
September 6, 2018 20:46
-
-
Save FirstWhack/ae311d7fdaf8866a1a47b9bfe8532c5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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