Created
October 25, 2017 23:27
-
-
Save ti-ka/005d423f570e72f57a1b499e2f64b4c0 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
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