Skip to content

Instantly share code, notes, and snippets.

@ahoward
Last active April 25, 2022 15:25
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/4394394 to your computer and use it in GitHub Desktop.
Save ahoward/4394394 to your computer and use it in GitHub Desktop.
<!--
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