F
F
fierro2019-03-14 12:57:58
JavaScript
fierro, 2019-03-14 12:57:58

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

3 answer(s)
C
coderisimo, 2019-03-14
@coderisimo

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");

V
Vladimir Proskurin, 2019-03-14
@Vlad_IT

Well, install node, and use https://nodejs.org/dist/latest-v6.x/docs/api/fs.html

I
Ilya Sevostyanov, 2019-03-14
@RUVATA

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 question

Ask a Question

731 491 924 answers to any question