Could you please help me with following issue.
Goal
Read file on client side (in browser via JS and HTML5 classes) line by line, without loading whole file to memory.
Scenario
I'm working on web page which should parse files on client side. Currently, I'm reading file as it described in this article.
HTML:
<input type="file" id="files" name="files[]" />
JavaScript:
$("#files").on('change', function(evt){ // creating FileReader var reader = new FileReader(); // assigning handler reader.onloadend = function(evt) { lines = evt.target.result.split(/\r?\n/); lines.forEach(function (line) { parseLine(...); }); }; // getting File instance var file = evt.target.files[0]; // start reading reader.readAsText(file); }
The problem is that FileReader reads whole file at once, which causes crashed tab for big files (size >= 300 MB). Using reader.onprogress
doesn't solve a problem, as it just increments a result till it will hit the limit.
Inventing a wheel
I've done some research in internet and have found no simple way to do this (there are bunch of articles describing this exact functionality but on server side for node.js).
As only way to solve it I see only following:
File.split(startByte, endByte)
method)But I'll better use something already existing to avoid entropy growth.
To read file contents on the client-side in JavaScript in various browsers, we add a file input and then read the select files from the input's value. to select the file input with getElementById . We get the first selected file with files[0] . Then we create a FileReader object.
JavaScript is actually interpreted line by line.
Use Plain JavaScript to Read a Local File Line by Line in JavaScript. We can create a simple JavaScript function to read the local file put in as the HTML Input. Copy document. getElementById('file').
To read a file, use FileReader , which enables you to read the content of a File object into memory. You can instruct FileReader to read a file as an array buffer, a data URL, or text. // Check if the file is an image.
Eventually I've created new line-by-line reader, which is totally different from previous one.
Features are:
Check this jsFiddle for examples.
Usage:
// Initialization var file; // HTML5 File object var navigator = new FileNavigator(file); // Read some amount of lines (best performance for sequential file reading) navigator.readSomeLines(startingFromIndex, function (err, index, lines, eof, progress) { ... }); // Read exact amount of lines navigator.readLines(startingFromIndex, count, function (err, index, lines, eof, progress) { ... }); // Find first from index navigator.find(pattern, startingFromIndex, function (err, index, match) { ... }); // Find all matching lines navigator.findAll(new RegExp(pattern), indexToStartWith, limitOfMatches, function (err, index, limitHit, results) { ... });
Performance is same to previous solution. You can measure it invoking 'Read' in jsFiddle.
GitHub: https://github.com/anpur/client-line-navigator/wiki
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With