简体   繁体   中英

Simply get each line in a .txt file into a javascript array

Thought this wouldn't be this difficult, but I'm simply trying to get the contents of a file and store each line into an array.

The 'file.txt' file (formatted as plain text):

file1.dwg
file2.dwg
file3.pdf
file4.dwg
file5.pdf

My javascript code:

function readTextFile('file.txt') {
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", 'file.txt', false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                alert(allText);
            }
        }
    }
    rawFile.send(null);
}

The syntax errors shown in the console:
- safari: SyntaxError: Unexpected string literal 'file.txt'. Expected a parameter pattern or a ')' in parameter list. SyntaxError: Unexpected string literal 'file.txt'. Expected a parameter pattern or a ')' in parameter list.
- chrome: Uncaught SyntaxError: Unexpected string

I'm running the files on a simple live server, with the following file structure:
- file.txt
- page.html
- script.js

The page.html just calls the script and is somewhere where I can work and open the console.

Why are the errors occurring, and how can they be fixed? I've tried to follow the instructions from this other post, but it has also failed: How to read a local text file?

Then I also tried the node solution and made my code like this:

var fs = require('fs');
var array = fs.readFileSync('file.txt').toString().split("\n");
for(i in array) {
    console.log(array[i]);
}

I know nothing about Node but gave it a go - the errors were:
- chrome: Uncaught ReferenceError: require is not defined
- safari: ReferenceError: Can't find variable: require

Thanks for any help here - I don't know how to fix this seemingly simple problem.

 function readTextFile(textFilePath) { //set a variable
     var rawFile = new XMLHttpRequest();
     rawFile.open("GET", textFilePath, false); //user variable here
     let fileNameArray= [];//defined array here for now
     rawFile.onreadystatechange = function (){
           if(rawFile.readyState === 4)
           {
              if(rawFile.status === 200 || rawFile.status == 0)
              {
                  var allText = rawFile.responseText;
                  console.log(allText); //check in broswer console

                  //alert(allText)
                  //or enale the alert function

                  fileNameArray = allText.split('\n'); //split by line break and add to array
                  console.log(fileNameArray)

               }
            }
      }
      rawFile.send(null);
 }

 readTextFile('./text.txt'); // call function and pass relative path of text file here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM