0

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

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

4

4 回答 4

2

HTML5 最终通过 File 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/

于 2012-02-23T12:41:23.227 回答
1

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

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

http://polyakoff.ucoz.net/

于 2016-06-07T08:36:19.937 回答
0
fs.read(fd, buffer, offset, length, position, [callback])

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

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

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

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

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

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

于 2012-02-23T12:38:40.110 回答
0

TextStream 和 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();
}
于 2012-02-23T13:14:58.610 回答