-1

I can have the user upload a file on a webpage using <input type='file' accept='text/plain' onchange='ReadTheTextfile(event)'>. and then use javascript: FileReader, reader.readAsText(event.target.files[0]); etc

but I already know that I want to read a file, which I already uploaded to the webserver.

How can I determine by myself which file I want to upload / read ? XMLHttpRequest ?

I don't want to read a file from the user's pc. I want to read a file from the server, where my html files are also hosted.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
tommer
  • 65
  • 1
  • 6
  • Possible duplicate of [reading server file with javascript](https://stackoverflow.com/questions/13329853/reading-server-file-with-javascript) – JJJ Dec 02 '18 at 12:58

1 Answers1

-2

You can retrive it via ajax call as follows.

function getFileFromServer(url, doneCallback) {
    var xhr;

    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = handleStateChange;
    xhr.open("GET", url, true);
    xhr.send();

    function handleStateChange() {
        if (xhr.readyState === 4) {
            doneCallback(xhr.status == 200 ? xhr.responseText : null);
        }
    }
}

getFileFromServer("path/to/file", function(text) {
    if (text === null) {
        // An error occurred
    }
    else {
        // `text` is the file text
    }
});

Reference - https://stackoverflow.com/a/13329900/9640177

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122