简体   繁体   English

只需将.txt文件中的每一行放入javascript数组

[英]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): “ file.txt”文件(格式为纯文本):

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

My javascript code: 我的JavaScript代码:

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. SyntaxError: Unexpected string literal 'file.txt'. Expected a parameter pattern or a ')' in parameter list.
- chrome: Uncaught SyntaxError: Unexpected string Uncaught SyntaxError: Unexpected string

I'm running the files on a simple live server, with the following file structure: 我正在具有以下文件结构的简单实时服务器上运行文件:
- file.txt -file.txt
- page.html -page.html
- script.js -script.js

The page.html just calls the script and is somewhere where I can work and open the console. page.html只是调用脚本,并且可以在其中工作并打开控制台。

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: 我对Node一无所知,但还是去了-错误是:
- chrome: Uncaught ReferenceError: require is not defined -chrome:未捕获的ReferenceError:require未定义
- safari: ReferenceError: Can't find variable: require -野生动物园:ReferenceError:找不到变量: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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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