Skip to content

Instantly share code, notes, and snippets.

@jdjkelly
Created March 2, 2012 14:51
Show Gist options
  • Save jdjkelly/1958888 to your computer and use it in GitHub Desktop.
Save jdjkelly/1958888 to your computer and use it in GitHub Desktop.
XHR2
<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