Javascript使用File.Reader()逐行读取 [英] Javascript using File.Reader() to read line by line

查看:1360
本文介绍了Javascript使用File.Reader()逐行读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题很接近但是不够接近。

我的HTML5应用程序读取一个CSV文件(虽然它也适用于文本)并在屏幕上显示一些数据。

My HTML5 application reads a CSV file (although it applies to text as well) and displays some of the data on screen.

我遇到的问题是CSV文件可能很大(我设法让业务同意1GB的文件大小限制)。好消息是,我只需要随时在CSV文件中显示部分数据。

The problem I have is that the CSV files can be huge (I'm managed to get business to agree a 1GB file size limit). The good news is, I only need to display some of the data from the CSV file at any point.

这个想法是这样的(例如) psudeo代码)

The idea is something like (psudeo code)

var content;
var reader =  OpenReader(myCsvFile)
var line = 0;

while (reader.hasLinesRemaning)
    if (line % 10 == 1)
      content = currentLine;
Loop to next line

有足够的文章介绍如何阅读CSV文件,I使用

There are enough articles about how to read the CSV file, I'm using

function openCSVFile(csvFileName){
    var r = new FileReader();
    r.onload = function(e) {
        var contents = e.target.result;
        var s = "";
    };  
    r.readAsText(csvFileName);
}

但是,我无法在Javascript中看到如何一行读取行或者即使它是可能的。

but, I can't see how to read line at a time in Javascript OR even if it's possible.

我的CSV数据看起来像

My CSV data looks like

Some detail: date, ,
More detail: time, ,
val1, val2
val11, val12
#val11, val12
val21, val22

我需要删除前两行,并考虑如何处理以#开头的行(因此我需要通读行一次)

I need to strip out the first 2 lines, and also consider what to do with the line starting with a # (hence why I need to read through line at a time)

因此,除了将该批次加载到内存中之外,我是否有任何选项可以一次读取行?

So, other than loading the lot into memory, do I have any options to read line at a time?

推荐答案

到目前为止,没有 readLine()方法。但是,有些想法值得探讨:

There is no readLine() method to do this as of now. However, some ideas to explore:


  • 从blob读取会触发 progress 事件。虽然规范不要求,但引擎可能过早填充 .result 属性,类似于XMLHttpRequest。

  • Streams API 草稿流媒体 .read(size)文件阅读器的方法。我不认为它已经在任何地方实现过了。

  • Blob 确实 a slice 方法,它返回包含原始数据的一部分的新Blob。操作的规范和同步性质表明这是通过引用完成的,而不是复制,并且应该是非常高效的。这将允许您逐块读取大块文件。

  • Reading from a blob does fire progress events. While it is not required by the specification, the engine might prematurely populate the .result property similar to an XMLHttpRequest.
  • The Streams API drafts a streaming .read(size) method for file readers. I don't think it is already implemented anywhere, though.
  • Blobs do have a slice method which returns a new Blob containing a part of the original data. The spec and the synchronous nature of the operation suggest that this is done via references, not copying, and should be quite performant. This would allow you to read the huge file chunk-by-chunk.

不可否认,这些方法都没有在行结束时自动停止。您需要手动缓冲块,将它们分成几行并在完成后将它们移出。此外,这些操作正在处理字节,而不是字符,因此可能存在需要处理的多字节字符的编码问题。

Admittedly, none of these methods do automatically stop at line endings. You will need to buffer the chunks manually, break them into lines and shift them out once they are complete. Also, these operations are working on bytes, not on characters, so there might be encoding problems with multi-byte characters that need to be handled.

另请参阅:在客户端的JavaScript中读取逐行文件

这篇关于Javascript使用File.Reader()逐行读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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