html5 fileReader -- how to only read the first N characters of a file?
You can use the .slice
method. You can read more here
var reader = new FileReader();
reader.onloadend = function(evt)
{
if (evt.target.readyState == FileReader.DONE) // DONE == 2
{
alert(evt.target.result);
}
};
var blob = file.slice(start, stop + 1);
reader.readAsBinaryString(blob);
Not enough rep to comment, so putting some warnings about @Stu Blair solution here:
With the Blob.slice
method you are taking the bytes from the Blob, not the characters.
For example, this won't work:
const blob = new Blob(['ð'], {type: 'text/plain'});
const fr = new FileReader();
fr.readAsText(blob); // Fine, fr.result will be 'ð'
fr.readAsText(blob.slice(0, 2)); // Not good, fr.result will be '��'
You will have to use FileReader.readAsArrayBuffer
to get the bytes. If your encoding is something like utf-8 you will have to read from the beginning.