Skip to content Skip to sidebar Skip to footer

How Can I Read A Text File Using Javascript?

How can I read a .txt file in Javascript? I have a folder name and need to load the file present in the folder (there is only one). For example, if I have a folder called folder1 i

Solution 1:

maybe this

var txtFile = newXMLHttpRequest();
txtFile.open("GET", "http://my.remote.url/myremotefile.txt", true);
txtFile.onreadystatechange = function() 
{
 if (txtFile.readyState === 4)   // Makes sure the document is ready to parse.
 {
  if (txtFile.status === 200)   // Makes sure it's found the file.
   {
    allText = txtFile.responseText; 
   }
 }
}

if the files are on he client computer there could be a problem cause of security reasons i dont that it is possible.

Solution 2:

This website is not here to do the job for you!

but, i'll point you the right way,

you can list the files in this folder and look for files with .txt extentions..

Solution 3:

//Assuming a text file full of integersvar fileArray = []
var file_selector = document.getElementById("file_selector");

file_selector.addEventListener("change", function(){
    var files_array = this.files;
//Now you have a reference to the file and you can do something with it.     var reader = newFileReader();
    reader.readAsText(this.files[0]); 
    type = "text";

    reader.onload = function(){
        array = reader.result.split(/\n/); 

        for (a in array) {
            //now turn your strings into integers
            array[a] = parseInt(array[a], 10); 
        }

        //Check your new arrayfor (var k = 0; k< array.length; k++){
            console.log(array[k]);
        }

    }

}); 

Post a Comment for "How Can I Read A Text File Using Javascript?"