Javascript filereader onload (get file from server)

I want to read a file from a Windows file system or server so that I can display content on a website, and we are not allowed to use a database or just Javascript in PHP.

What I have now is below, and it works if I get the file from the boot box of the HTML file, the only thing I need is how to get the file in javascript without manually inserting it, and upload it to pageload.

The rest of the code works, if I insert the file manually, I only need to get the file and paste it into the var = file;

var file = // How do I get file from windows system / or server is also a possibility

var reader = new FileReader();
reader.onload = function(progressEvent){
// Entire file
console.log(this.result);

// By lines
var lines = this.result.split('\n');
for(var line = 0; line < lines.length; line++){
  console.log(lines[line]);
  }
};
reader.readAsText(file);
0
source share
2 answers
I got it to work


 var file = readTextFile("test.txt");
 var allText;
 var trumpCount = 0;
 var hilaryCount = 0;
 var reader = new FileReader();
// Entire file
console.log(this.result);
 // alert(allText);
// By lines
var lines = allText.split('\n');
for(var line = 0; line < lines.length; line++){
 // alert(lines[line]);
  if (lines[line].indexOf("t") !== -1){
    trumpCount++;
  }else{
  hilaryCount++;
  }

}
alert("Votes for trump: " + trumpCount +  " Votes for hilary: " + hilaryCount + " Total votes: " + (trumpCount + hilaryCount))

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)
        {
        // Doe hier je shit
           allText = rawFile.responseText;
            //alert(allText);
        }
    }
}
rawFile.send(null);
}
0
source

- .

File FileList, , DataTransfer API mozGetAsFile() HTMLCanvasElement.

https://developer.mozilla.org/nl/docs/Web/API/FileReader

0

Source: https://habr.com/ru/post/1684631/


All Articles