Chunked file read
by Arjan Haverkamp
HTML
<input type="file" id="file-input">
JavaScript
const SEP = '~=-=-=-=-=-=-=-=-|||PhideoFrame|||=-=-=-=-=-=-=-=-=-=~';
const input = document.getElementById('file-input');
input.addEventListener('change', async () => {
const file = input.files[0];
const chunkSize = 147, nrChunks = Math.ceil(file.size / chunkSize);
let chunkStart = 0, chunkEnd = 0, chunk, txt;
let firstBit = '', lastBit = '';
let chunks = [];
while(chunkEnd < file.size) {
chunkEnd = chunkStart + chunkSize;
chunk = file.slice(chunkStart, chunkEnd);
txt = await chunk.text();
// console.log('TXT', txt, txt.length);
firstBit = txt.substring(0,SEP.length);
if (chunkStart > 0 && (lastBit + firstBit).indexOf(SEP) > -1) {
//console.log('!!! OVER');
// Separator goes over chunk boundary, back up a bit
chunkStart -= SEP.length;
chunkEnd -= SEP.length;
txt = lastBit + txt.substring(0, txt.length - SEP.length);
//console.log('NEW', txt, txt.length);
firstBit = txt.substring(0,SEP.length);
let prev = chunks.length - 1;
chunks[prev] = chunks[prev].substring(0,chunks[prev].length-SEP.length);
}
chunks.push(txt);
lastBit = txt.slice(txt.length - SEP.length);
chunkStart = chunkEnd;
} // End for
console.log(chunks);
const parts = [];
let splitPart = '';
for (let chunk of chunks) {
chunk = splitPart + chunk;
const subs = chunk.split(SEP);
splitPart = subs[subs.length-1];
for (i = 0; i < subs.length - 1; i++) {
parts.push(subs[i])
}
}
parts.push(splitPart);
console.log(parts);
/*
// Now split this by SEP
const parts = []; let part = '';
for (let chunk of chunks) {
const subs = chunk.split(SEP);
console.log('subs',subs)
let idx = chunk.indexOf(SEP);
if (idx > -1) {
part += chunk.substring(0,idx);
parts.push(part);
part = chunk.substring(idx + SEP.length);
}
...