【问题标题】:How to read a huge text file through javascript or jquery?如何通过 javascript 或 jquery 读取巨大的文本文件?
【发布时间】:2012-02-23 12:10:34
【问题描述】:

如何通过 javascript 或 jquery 逐行读取巨大的文本文件? 我无法全部读取并拆分为数组,因为它需要大量内存。我只想直播...

编辑 作为说明,我正在开发一个 google chrome 扩展程序,因此使用 fso ActiveX 的解决方案在此浏览器上不起作用。还有其他想法吗?

【问题讨论】:

  • 你确定 jquery 是适合这项工作的工具吗?
  • 这里提出了类似的问题。 stackoverflow.com/questions/585234/…
  • 我正在为 chrome 编写扩展程序。我认为它应该以这种方式在客户端购买中工作,它不会强制服务器。我可以使用 php 的其他语言是什么?
  • 文本文件将是存储在网络地址或客户端计算机中的文件......
  • 最好使用带有文件流的 java servlet..

标签: javascript jquery text-files line-by-line


【解决方案1】:

HTML5 最终通过文件 API 规范提供了一种与本地文件交互的标准方式。作为其功能的示例,文件 API 可用于在将图像发送到服务器时创建图像的缩略图预览,或允许应用程序在用户离线时保存文件引用。此外,您可以使用客户端逻辑来验证上传的 mimetype 是否匹配其文件扩展名或限制上传的大小。

规范提供了几个用于从“本地”文件系统访问文件的接口: 1.File - 单个文件;提供只读信息,例如名称、文件大小、mimetype 和对文件句柄的引用。 2.FileList - 一个类似数组的 File 对象序列。 (思考或从桌面拖动文件目录)。 3.Blob - 允许将文件分割成字节范围。

当与上述数据结构结合使用时,FileReader 接口可用于通过熟悉的 JavaScript 事件处理异步读取文件。因此,可以监视读取的进度、捕获错误并确定加载何时完成。在许多方面,API 类似于 XMLHttpRequest 的事件模型。

注意:在编写本教程时,Chrome 6.0 和 Firefox 3.6 支持处理本地文件所需的 API。从 Firefox 3.6.3 开始,不支持 File.slice() 方法。

http://www.html5rocks.com/en/tutorials/file/dndfiles/

【讨论】:

  • 对于 HTML5,问题是读取代码中指定的文件。 HTML5 允许通过 读取文件。但是我需要在代码中指定文件。
【解决方案2】:

惰性文本视图小部件旨在在网页上显示文本。关键特性是它不会将整个文本加载到浏览器内存中,而是仅显示文件的片段(帧)。这允许显示大的、非常大的、巨大的文本。

他的小部件提供用于文本显示的用户界面并需要服务器端数据源。你必须自己实现服务器端组件,它的逻辑很简单。当小部件需要下一个文本块时,它会向服务器(使用 POST 方法)查询下一个块。

http://polyakoff.ucoz.net/

【讨论】:

    【解决方案3】:
    fs.read(fd, buffer, offset, length, position, [callback])
    

    从 fd 指定的文件中读取数据。

    buffer 是数据将被写入的缓冲区。

    offset 是缓冲区内开始写入的偏移量。

    length 是一个整数,指定要读取的字节数。

    position 是一个整数,指定从文件中的何处开始读取。如果 position 为 null,则从当前文件位置读取数据。

    http://nodejs.org/docs/v0.4.8/api/fs.html#file_System

    【讨论】:

    • 不幸的是,我读到 Google chrome 不支持 FSO activeX 还有其他想法吗?
    【解决方案4】:

    TextStream and Scripting.FileSystemObject

    ; object = ObjectOpen("Scripting.FileSystemObject") ; WIL syntax
    ; ObjectClose(object)                               ; WIL syntax
    ;
    ; TextStream = object.CreateTextFile(filename[, overwrite[, unicode]])      ; Creates a file as a TextStream
    ; TextStream = object.OpenTextFile(filename[, iomode[, create[, format]]])  ; Opens a file as a TextStream
    ;
    ; TextStream.Close                       ; Close a text stream.
    ;
    ; TextStream.ReadAll                     ; Read the entire stream into a string.
    ; TextStream.ReadLine                    ; Read an entire line into a string.
    ; TextStream.Read (n)                    ; Read a specific number of characters into a string.
    ; 
    ; TextStream.Write (string)              ; Write a string to the stream.
    ; TextStream.WriteLine                   ; Write an end of line to the stream.
    ; TextStream.WriteLine (string)          ; Write a string and an end of line to the stream.
    ; TextStream.WriteBlankLines (n)         ; Write a number of blank lines to the stream.
    ; 
    ; TextStream.SkipLine                    ; Skip a line.
    ; TextStream.Skip (n)                    ; Skip a specific number of characters.
    ; 
    ; TextStream.Line                        ; Current line number.
    ; TextStream.Column                      ; Current column number.
    ; 
    ; TextStream.AtEndOfLine                 ; Boolean Value. Is the current position at the end of a line?
    ; TextStream.AtEndOfStream               ; Boolean Value. Is the current position at the end of the stream?
    ; -------------------------------------------------------------------------------------------------------------------------------
    

    示例代码:

    function ReadFiles()
    {
      var fso, f1, ts, s;
      var ForReading = 1;
      fso = new ActiveXObject("Scripting.FileSystemObject");
      f1 = fso.CreateTextFile("c:\\testfile.txt", true);
      // Write a line.
      Response.Write("Writing file <br>");
      f1.WriteLine("Hello World");
      f1.WriteBlankLines(1);
      f1.Close();
      // Read the contents of the file.
      Response.Write("Reading file <br>");
      ts = fso.OpenTextFile("c:\\testfile.txt", ForReading);
      s = ts.ReadLine();
      Response.Write("File contents = '" + s + "'");
      ts.Close();
    }
    

    【讨论】:

    • 不幸的是,我读到 Google chrome 不支持 FSO activeX 还有其他想法吗?
    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多