Skip to content

Instantly share code, notes, and snippets.

@ti-ka
Created October 25, 2017 23:27
Show Gist options
  • Save ti-ka/005d423f570e72f57a1b499e2f64b4c0 to your computer and use it in GitHub Desktop.
Save ti-ka/005d423f570e72f57a1b499e2f64b4c0 to your computer and use it in GitHub Desktop.
public exportFile(filename: string, type: string, data: any) {
const blob = new Blob([data], {type: type});
this.exportBlob(blob, filename);
}
public exportBlob(blob: Blob, filename: string) {
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
} else {
const elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}
public downloadFile(url: string, filename: string, type: string) {
const self = this;
const oReq = new XMLHttpRequest();
oReq.addEventListener('load', function (response) {
console.log(response);
const blob = new Blob([oReq.response], {type: type});
self.exportBlob(blob, filename);
});
oReq.responseType = 'arraybuffer';
oReq.open('GET', url);
oReq.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment