Created
March 18, 2017 09:05
-
-
Save abhisuri97/a9b5e172e44d9d2771c1df1015b92e3d to your computer and use it in GitHub Desktop.
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
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