0

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.
- 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.

user8758206
  • 2,106
  • 4
  • 22
  • 45
  • `function readTextFile('file.txt')` function parameters are for passing parameters.. – Keith May 08 '19 at 22:48
  • In the directory where you have the node file.Did you do : npm init && npm install – Atish Shakya May 09 '19 at 02:31
  • re @Keith but in this other post it's the same: https://stackoverflow.com/questions/14446447/how-to-read-a-local-text-file - how can i fix this? – user8758206 May 09 '19 at 06:13
  • re @AtishShakya no - do I have to download Node and run something in the command line? (I'm not used to Node) Could you link to some instructions about this method? Thank you – user8758206 May 09 '19 at 06:14
  • 1
    `but in this other post it's the same` No it's not, it's `function readTextFile(file)`, that's different.. – Keith May 09 '19 at 08:44

1 Answers1

1
 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
Atish Shakya
  • 551
  • 6
  • 14
  • thank you! yes, you're right - i can see what I did wrong now. Of course - the function just passes in the variable that's defined 'text,txt'! – user8758206 May 09 '19 at 21:29