Reputation: 99
How can I read a huge text file line by line through javascript or jquery? I cant read all and split to an array because it will require lots of memmory. I just want to stream it...
EDIT As a note I am working on a google chrome extension so that solutions with fso ActiveX does not work on this browser. Any other ideas?
Upvotes: 0
Views: 10080
Reputation: 11
Lazy Text View widget is intended to display text on a web-page. The key feature is that it does not load whole text in the browser memory, but it displays only fragment (frame) of file. This allows to display large, very large, huge texts.
he widget provides user interface for text display and requires server-side data source. You have to implement server-side component yourself, it's logic is quite simple. When the widget needs next chunk of text, it queries server (using POST-method) for the next chunk.
Upvotes: 1
Reputation: 23113
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?
; -------------------------------------------------------------------------------------------------------------------------------
Sample Code:
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();
}
Upvotes: 0
Reputation: 23113
HTML5 finally provides a standard way to interact with local files, via the File API specification. As example of its capabilities, the File API could be used to create a thumbnail preview of images as they're being sent to the server, or allow an app to save a file reference while the user is offline. Additionally, you could use client-side logic to verify an upload's mimetype matches its file extension or restrict the size of an upload.
The spec provides several interfaces for accessing files from a 'local' filesystem: 1.File - an individual file; provides readonly information such as name, file size, mimetype, and a reference to the file handle. 2.FileList - an array-like sequence of File objects. (Think or dragging a directory of files from the desktop). 3.Blob - Allows for slicing a file into byte ranges.
When used in conjunction with the above data structures, the FileReader interface can be used to asynchronously read a file through familiar JavaScript event handling. Thus, it is possible to monitor the progress of a read, catch errors, and determine when a load is complete. In many ways the APIs resemble XMLHttpRequest's event model.
Note: At the time of writing this tutorial, the necessary APIs for working with local files are supported in Chrome 6.0 and Firefox 3.6. As of Firefox 3.6.3, the File.slice() method is not supported.
http://www.html5rocks.com/en/tutorials/file/dndfiles/
Upvotes: 2
Reputation: 23113
fs.read(fd, buffer, offset, length, position, [callback])
Read data from the file specified by fd.
buffer is the buffer that the data will be written to.
offset is offset within the buffer where writing will start.
length is an integer specifying the number of bytes to read.
position is an integer specifying where to begin reading from in the file. If position is null, data will be read from the current file position.
http://nodejs.org/docs/v0.4.8/api/fs.html#file_System
Upvotes: 0