Luke Luke - 7 days ago 6
Javascript Question

How to handle a large text file in javascript need a slicing algorithm

I'm trying to get my head around splicing a file, and whilst I can read certain parts of the file as per the tutorial here.

I can't work out how to write a loop, so that I read the entire file in chunks.

Needless to say I feel I'm well off the mark but here's the code I've been working on.

var loops = file.size / 10;
var start = 0;
var stop = file.size / 10;

for (var i = 0; i < loops; i++) {
var blob = file.slice(start, stop + 1);
reader.readAsText(blob);
start = stop;
stop += stop;
}


I'm trying to read the file in chunks 1 10th of its total size. By starting with the bytes range 0 - 1/10th of the size, then moving up to have my start byte at the position of the end byte and doubling the end byte, but I realise this isn't the correct way to be doing this,

I've got hold of the file:

File {webkitRelativePath: "", lastModifiedDate: Thu Nov 07 2013 07:56:00 GMT+0000 (GMT), name: "TestData.txt", type: "text/plain", size: 136000000…}


but this code gives me an error about my reader not being usable at this time...

Answer

You have to calculate the length of the chunks and add that length to stop at the end of every loop.

var loops = 10;
var chunkLength = file.size / loops;
var start = 0;
var stop = chunkLength;

for (var i = 0; i < loops; i++) {
  var blob = file.slice(start, stop + 1);
  reader.readAsText(blob);
  start = stop;
  stop += chunkLength;
}
Comments