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

前端 未结 4 492
眼角桃花
眼角桃花 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:31

    With jQuery:

    myObject = {}; //myObject[numberline] = "textEachLine";
    $.get('path/myFile.txt', function(myContentFile) {
       var lines = myContentFile.split("\r\n");
    
       for(var i  in lines){
          //here your code
          //each line is "lines[i]"
    
          //save in object "myObject": 
          myObject[i] = lines[i]
    
          //print in console
          console.log("line " + i + " :" + lines[i]);
       }
    }, 'text');
    

提交回复
热议问题