I have a web page made by html+javascript which is demo, I want to know how to read a local csv file and read line by line so that I can extract data from the csv file.
Asked
Active
Viewed 2.8e+01k times
98
-
2Check this out http://www.html5rocks.com/en/tutorials/file/dndfiles/ – Hunter Larco Apr 28 '14 at 02:23
-
2do you have any browser compatibility requirements? specifically ddo you support ie9 or less? – undefined Apr 28 '14 at 02:23
-
https://developer.mozilla.org/en-US/docs/Web/API/FileReader – Derek 朕會功夫 Apr 28 '14 at 02:44
-
@HunterLarco thank you, the problem is that I don't know how to get each line from the result. I mean reader.readAsText() returns all the data instead of I can read line by line – litaoshen Apr 28 '14 at 02:54
-
@LukeMcGregor No requirements, just support the current versions will be OK. – litaoshen Apr 28 '14 at 02:55
-
@litaoshen No problem. You could try splitting the result by '\n', being the new line character. – Hunter Larco Apr 28 '14 at 02:56
-
@Derek朕會功夫 Still, how to get contents line by line, because it seems that readAsText will return all the data – litaoshen Apr 28 '14 at 02:57
-
@HunterLarco, OK, just like C string style – litaoshen Apr 28 '14 at 03:03
-
@litaoshen - Here's a related post that might answer your question: http://stackoverflow.com/questions/9917246/how-do-you-read-parse-a-text-file-line-by-line-using-html-javascript?rq=1 – Derek 朕會功夫 Apr 28 '14 at 04:21
-
A better tested and more production-quality solution is at http://stackoverflow.com/questions/24647563/reading-line-by-line-file-in-javascript-on-client-side. @Derek: no, that answer you mentioned doesn't help. – Dan Dascalescu Sep 16 '15 at 02:27
2 Answers
137
Without jQuery:
const $output = document.getElementById('output')
document.getElementById('file').onchange = function() {
var file = this.files[0];
var reader = new FileReader();
reader.onload = function(progressEvent) {
// Entire file
const text = this.result;
$output.innerText = text
// By lines
var lines = text.split('\n');
for (var line = 0; line < lines.length; line++) {
console.log(lines[line]);
}
};
reader.readAsText(file);
};
<input type="file" name="file" id="file">
<div id='output'>
...
</div>
Remember to put your javascript code after the file field is rendered.
-
32I have 200000 lines (not kidding, it's a log file). I don't think your solution covers that, nice try though. – Tomáš Zato Oct 23 '15 at 15:16
-
Also, this solution doesn't handle if that return(line feed) is within a quoted field. As for Tomas, if you have a more advanced browser, you could use a generator to read line by line without doing a "split". – Rahly Dec 11 '15 at 13:12
-
8
-
@TomášZato mine is 100m lines. I haven't tested that answer yet though.. How did you approach that? A link to an example would be super-appreciated! huanPastas, +1 for the answer! – gsamaras Sep 06 '16 at 03:36
-
2@gsamaras I don't remember exactly but I used some stream that reads data piece by piece and then emmited event for every time I encountered `\n`. But with 100m lines, you're gonna run into table with displaying them in HTML. – Tomáš Zato Sep 06 '16 at 07:58
-
I've been looking everywhere for a way to do this and this is exactly what I needed. God bless your soul. – rpalo Jan 06 '17 at 22:56
-
Can we read a file onload of the script without input tag solely in javascript if we provide the file path in it ? – Adarsh Singh Jan 16 '20 at 12:31
-
@TomášZato-ReinstateMonica In fact, I just ran this script on a 60.000.000+ line file, and it was perfectly smooth :) Longest part was the upload. – zessx Oct 05 '20 at 13:45
-
@zessx I actually once wrote a generator function that does not split the original string and serves lines one after another, but I cannot find it. – Tomáš Zato Oct 05 '20 at 16:29
46
Using ES6 the javascript becomes a little cleaner
handleFiles(input) {
const file = input.target.files[0];
const reader = new FileReader();
reader.onload = (event) => {
const file = event.target.result;
const allLines = file.split(/\r\n|\n/);
// Reading line by line
allLines.forEach((line) => {
console.log(line);
});
};
reader.onerror = (event) => {
alert(event.target.error.name);
};
reader.readAsText(file);
}

JuJoDi
- 14,627
- 23
- 80
- 126
-
5voted up for split lines with regex which is the right way of doing it. – Meysam Feghhi Oct 10 '17 at 21:10
-
17
-
3Excellent example, and I love that Windows and Unix-style line endings are handled. Thank you. – Brad Mar 29 '19 at 18:14
-
5`const allLines = file.split(/\r\n|\n/);` - This is not really "read line by line". This is gulping the whole multi-gig file and choking on it. – Ark-kun Apr 21 '22 at 07:27
-
1
-
@mke21 Please see this answer: https://stackoverflow.com/a/71983228/1497385 – Ark-kun May 30 '22 at 00:18