0

I figured out how to read one text file, but I am not sure how to read multiple text files in a local folder.

Using the code below, I can read a text file:

function readText(file) {
    var text = '';
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function() {
        if (rawFile.readyState === 4 && rawFile.status === 200) {
            text = rawFile.responseText;
        }
    }
    rawFile.send(null);
    return text;
}

Can anyone please help me figure out how I can do this for multiple files in one folder?

I do not know in advance what files will be in the folder, so I cannot simply repeat the above function multiple times.

Daniel
  • 77
  • 1
  • 1
  • 8
  • 2
    You can't get the folder contents, you can only fetch files you actually know the filename for. Note that you shouldn't be doing synchronous ajax. – adeneo Jul 01 '16 at 21:20
  • Maybe implement a service that responds to HTTP requests with the filenames in the directory? – Axel F Jul 01 '16 at 21:22
  • I realized that I can get HTML format of folder by `var files = new XMLHttpRequest(); files.open("GET", "folder name", false); files.send(null);` Then I can extract file names from here. But is this a bad approach? – Daniel Jul 01 '16 at 22:02

0 Answers0