Last active
April 25, 2022 15:25
-
-
Save ahoward/4394394 to your computer and use it in GitHub Desktop.
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
<!-- | |
doing a large chunked upload of content using html5's file input feature is tricky. | |
this simple example should help you out. | |
--> | |
<br> | |
<br> | |
<br> | |
<hr> | |
<center> | |
<blink> | |
<input type='file' name='file' id='file' /> | |
<br> | |
<div id='progress' /> | |
</blink> | |
</center> | |
<script> | |
jQuery('#file').change(function(){ | |
var file = document.getElementById('file').files[0]; | |
var progress = jQuery('#progress'); | |
if(file){ | |
var reader = new FileReader(); | |
var size = file.size; | |
var chunk_size = Math.pow(2, 13); | |
var chunks = []; | |
var offset = 0; | |
var bytes = 0; | |
reader.onloadend = function(e){ | |
if (e.target.readyState == FileReader.DONE){ | |
var chunk = e.target.result; | |
bytes += chunk.length; | |
chunks.push(chunk); | |
progress.html(chunks.length + ' chunks // ' + bytes + ' bytes...'); | |
if((offset < size)){ | |
offset += chunk_size; | |
var blob = file.slice(offset, offset + chunk_size); | |
reader.readAsText(blob); | |
} else { | |
progress.html("processing teh content..."); | |
var content = chunks.join(""); | |
alert("content is ready!"); | |
debugger; | |
}; | |
} | |
}; | |
var blob = file.slice(offset, offset + chunk_size); | |
reader.readAsText(blob); | |
} | |
}); | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment