- cors must be enabled.
-
-
Save wemersonjanuario/3c5a574261df7456edf192a3e336ab25 to your computer and use it in GitHub Desktop.
Download file with xhr2 [blob url & html5 file api]
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://backend.siplik.co/public/word-to-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) { | |
window.open(href=window.URL.createObjectURL( | |
new Blob([this.response], {type: 'application/pdf'}) | |
)); | |
} | |
}; | |
xhr.send(); |
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://backend.siplik.co/public/word-to-pdf.pdf'; | |
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; | |
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) { | |
window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function (fs) { | |
fs.root.getFile('q.pdf', { create: true }, function (fileEntry) { | |
fileEntry.createWriter(function (writer) { | |
writer.onwriteend = function (e) { | |
window.open(fileEntry.toURL()); | |
console.log('archivo escrito'); | |
} | |
writer.onwrite = function(e) { console.log(e); }; | |
writer.onerror = function(e) { console.log(e); }; | |
var blob = new Blob([xhr.response], {type: 'application/pdf'}); | |
writer.write(blob); | |
}, function (e) { | |
console.log('cw'); | |
console.log(e); | |
}); | |
}, function (e) { | |
console.log('fs'); | |
console.log(e); | |
}); | |
}, function (e) { | |
console.log('rfs'); | |
console.log(e); | |
}); | |
} | |
}; | |
xhr.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment