js read binary file code example
Example: javascript read binary file
function fileFilter(file, fnLineFilter, fnComplete) {
var bPos = 0,
mx = file.size,
BUFF_SIZE = 262144,
i = 0,
collection = [],
lineCount = 0;
var d1 = +new Date;
var remainder = "";
function grabNextChunk() {
var myBlob = file.slice(BUFF_SIZE * i, (BUFF_SIZE * i) + BUFF_SIZE, file.type);
i++;
var fr = new FileReader();
fr.onload = function(e) {
var str = remainder + e.target.result,
o = str,
r = str.split(/\r?\n/);
remainder = r.slice(-1)[0];
r.pop();
lineCount += r.length;
var rez = r.map(fnLineFilter).filter(Boolean);
if (rez.length) {
[].push.apply(collection, rez);
}
if ((BUFF_SIZE * i) > mx) {
fnComplete(collection);
console.log("filtered " + file.name + " in " + (+new Date() - d1) + "ms ");
}
else {
setTimeout(grabNextChunk, 0);
}
};
fr.readAsText(myBlob, myBlob.type);
}
grabNextChunk();
}