KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Currently I use a pattern like the following to read the first 3 characters of a series of files: var files = e.dataTransfer.files; for (var i = 0, f; f = files[i]; i++) { var fr = new FileReader(); fr.onload = function(e) { var first_three_chars = e.target.result.substr(0,3); } fr.readAsText(f); } The trouble is that I'm only interested in the first 3 characters of the file, whereas this method reads the entire file, wasting lots of memory and time. How can I quickly iterate over the files, simply taking quick peeks at the first characters? Edit: slice() was the answer, thanks sshen. Here's how I did it: var files = e.dataTransfer.files; for (var i = 0, f; f = files[i]; i++) { var fr = new FileReader(); fr.onloadend = function(e) { if (e.target.readyState == FileReader.DONE) { var first_three_chars = e.target.result; } }; var blob = f.slice(0, 3); fr.readAsText(blob); }
Tags (comma-separated)
Save Edits
Cancel