-
-
Save wemersonjanuario/45d302337b45d6aad866051f0473c646 to your computer and use it in GitHub Desktop.
Ajax blob save as.. file download
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
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
(y)