Answer the question
In order to leave comments, you need to log in
How to read text file using Javascript?
Hello. There is a need to process a text file line by line, for example, as it is done in C#. Unfortunately, I don't have much time to set up a server and I'm not good at it, so PHP is not an option. Can this work be done in Javascript?
Answer the question
In order to leave comments, you need to log in
Try like this :
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
alert(allText);
}
}
}
rawFile.send(null);
}
readTextFile("file:///C:/your/path/to/file.txt");
Well, install node, and use https://nodejs.org/dist/latest-v6.x/docs/api/fs.html
Are you interested in working with JS in a browser environment?
If not, then you need Node.JS
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question