如何在JavaScript中按行读取文件? [英] How can you read a file line by line in JavaScript?

查看:274
本文介绍了如何在JavaScript中按行读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个iPad的网络应用程序,将从文本文件加载数据。 (样本数据集大约为〜400 kb)。除了文件阅读,我已经设置了一切。我已经建立了我的代码,你传递一个逐行读取文件的对象。



如何逐行读取文件?



如果没有直接的方法来逐行读取文件,有人可以给我看一个如何将文件读入字符串对象的例子吗? (这样我就可以使用split方法:P)

解决方案

如果我明白你想要做什么,

  var txtFile = new XMLHttpRequest(); 
txtFile.open(GET,http://website.com/file.txt,true);
txtFile.onreadystatechange = function()
{
if(txtFile.readyState === 4){//文档准备好解析。
if(txtFile.status === 200){//找到文件
allText = txtFile.responseText;
lines = txtFile.responseText.split(\\\
);
}
}
}
txtFile.send(null);


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 way I have set up my code, you pass an object which reads a file line by line.

How can I read a file line by line?

If there is no direct way to read a file line by line, can someone please show me an example of how to read a file into a string object? (so that I can use the split method :P)

解决方案

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);

这篇关于如何在JavaScript中按行读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆