Created
March 2, 2012 14:51
-
-
Save jdjkelly/1958888 to your computer and use it in GitHub Desktop.
XHR2
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
<progress min="0" max="100" value="0">0% complete</progress> | |
<script> | |
function upload(blobOrFile) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', '/server', true); | |
xhr.onload = function(e) { ... }; | |
// Listen to the upload progress. | |
var progressBar = document.querySelector('progress'); | |
xhr.upload.onprogress = function(e) { | |
if (e.lengthComputable) { | |
progressBar.value = (e.loaded / e.total) * 100; | |
progressBar.textContent = progressBar.value; // Fallback for unsupported browsers. | |
} | |
}; | |
xhr.send(blobOrFile); | |
} | |
// Take care of vendor prefixes. | |
BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder; | |
var bb = new BlobBuilder(); | |
bb.append('hello world'); | |
upload(bb.getBlob('text/plain')); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment