2012-02-23 65 views
0

如何通过javascript或jquery逐行读取大型文本文件? 我无法读取所有内容并将其拆分为数组,因为它需要很多内存。我只是想流...如何通过javascript或jquery读取一个巨大的文本文件?

编辑 作为一个说明,我正在研究一个谷歌浏览器扩展,以便使用fso ActiveX解决方案不适用于此浏览器。任何其他想法?

+2

你肯定jQuery是这个职位的合适的工具? – Filburt 2012-02-23 12:13:02

+0

类似的问题在这里问。 http://stackoverflow.com/questions/585234/how-to-read-and-write-into-file-using-javascript – 2012-02-23 12:15:51

+0

我正在写一个扩展的铬。我认为它应该在客户端购买这种方式,它不会强制服务器。什么可以是我可以使用PHP的其他语言? – user1141820 2012-02-23 12:16:54

回答

0
fs.read(fd, buffer, offset, length, position, [callback]) 

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

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

偏移量将在开始写入的缓冲区内偏移。

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

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

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

+0

不幸的是我读到谷歌浏览器不支持FSO activeX任何其他想法? – user1141820 2012-02-29 03:18:26

2

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

该规范提供了几个接口来访问“本地”文件系统中的文件: 1.文件 - 单个文件;提供只读信息,如名称,文件大小,MIME类型和对文件句柄的引用。 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/

+0

对于HTML5,问题是读取代码中指定的文件。 HTML5允许通过读取文件。不过,我需要在代码中指定文件。 – user1141820 2012-02-29 03:18:00

0

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(); 
} 
+0

不幸的是,我读到谷歌浏览器不支持FSO activeX任何其他想法? – user1141820 2012-02-29 03:19:59

+0

您是否阅读过我的与HTML5相关的其他解决方案? – 2012-02-29 05:48:48

1

懒文本视图控件旨在显示网页上的文字。关键特性是它不会在浏览器内存中加载整个文本,但它只显示文件的片段(帧)。这允许显示大的,非常大的,巨大的文本。

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

http://polyakoff.ucoz.net/

相关问题