How can you read a file line by line in JavaScript?

前端 未结 4 487
眼角桃花
眼角桃花 2020-12-03 15:38

I\'m writing a web-app for the iPad that will be loading data from a text file. (A sample data set is around ~400 kb). I have everything set up except the file reading. The

4条回答
  •  借酒劲吻你
    2020-12-03 16:19

    This could work, if I understood what you want to do:

    var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "http://website.com/file.txt", true);
    txtFile.onreadystatechange = function()
    {
      if (txtFile.readyState === 4) {  // document is ready to parse.
        if (txtFile.status === 200) {  // file is found
          allText = txtFile.responseText; 
          lines = txtFile.responseText.split("\n");
        }
      }
    }
    txtFile.send(null);
    

提交回复
热议问题