Skip to content

Instantly share code, notes, and snippets.

@abhisuri97
Created March 18, 2017 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhisuri97/a9b5e172e44d9d2771c1df1015b92e3d to your computer and use it in GitHub Desktop.
Save abhisuri97/a9b5e172e44d9d2771c1df1015b92e3d to your computer and use it in GitHub Desktop.
function parseFile(file, oboeInstance) {
var fileSize = file.size;
var chunkSize = 512 * 1024; // 512 kb
var offset = 0;
var self = this; // we need a reference to the current object
var chunkReaderBlock = null;
var readEventHandler = function(evt) {
if (evt.target.error == null) {
offset += evt.target.result.length;
var chunk = evt.target.result;
oboeInstance.emit('data', chunk); // callback for handling read chunk
} else {
console.log("Read error: " + evt.target.error);
return;
}
if (offset >= fileSize) {
os.emit('done');
console.log("Done reading file");
}
// of to the next chunk
chunkReaderBlock(offset, chunkSize, file);
}
chunkReaderBlock = function(_offset, length, _file) {
var r = new FileReader();
var blob = _file.slice(_offset, length + _offset);
r.onload = readEventHandler;
r.readAsText(blob);
}
// now let's start the read with the first block
chunkReaderBlock(offset, chunkSize, file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment