-
-
Save nor1c/19cc7df6daa9f26d0a4889220a4a2d4f to your computer and use it in GitHub Desktop.
Ajax blob save as.. file download
This file contains hidden or 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
| var url = 'http://www.pdf995.com/samples/pdf.pdf'; | |
| var fileName = 'pdf.pdf'; | |
| var xhr = new XMLHttpRequest(); | |
| xhr.open('GET', url, true); | |
| xhr.responseType = 'blob'; | |
| xhr.onprogress = function(pe) { | |
| console.log('progress'); | |
| if (pe.lengthComputable) { | |
| console.log((pe.loaded / pe.total) * 100); | |
| } | |
| }; | |
| xhr.onload = function(e) { | |
| if (this.status == 200) { | |
| var blob = this.response; | |
| //TODO fallback needed for IE8 & IE9 | |
| if (navigator.appVersion.toString().indexOf('.NET') > 0) { | |
| //IE 10+ | |
| window.navigator.msSaveBlob(blob, fileName); | |
| } else { | |
| //Firefox, Chrome | |
| var a = document.createElement("a"); | |
| var blobUrl = window.URL.createObjectURL(new Blob([blob], {type: blob.type})); | |
| document.body.appendChild(a); | |
| a.style = "display: none"; | |
| a.href = blobUrl; | |
| a.download = fileName ; | |
| a.click(); | |
| } | |
| } | |
| }; | |
| xhr.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment