1

I want to do some basic calculations based on a text file. However since it's for a client it needs to be straight forward. Is it possible to do these calculations purely in the client. If somebody selects a file to upload through a form. Can I instead capture that through Javascript and process it that way?

1 Answer 1

8

Matti is correct, with HTML 4 you're out of luck.

However, with HTML 5 this is made possible by using the FileReader API. Currently support for this feature is limited to very recent versions of Chrome, Firefox and Opera. A similar feature exists in older versions of Firefox and this can also be done in older versions of Internet Explorer by using ActiveX. I would imagine that Google Gears also would allow this although I haven't looked into it.

The Javascript to do this would look like:

$( "#files" ).change( function( event ) { var reader = new FileReader(); reader.onload( e ) { /* handle a file being loaded. contents in event.result */ }; reader.onerror( e ) { /* handle an error during the file reading */ }; var files = event.target.files; for( var i = 0; i < files.length; i++ ) console.log(reader.readAsText( files[i] )); }); 

Where #files is simply:

<input type="file" id="files" multiple /> 
Sign up to request clarification or add additional context in comments.

3 Comments

I don't know if things have changed since this was written but I see results in event.target.result rather than event.result.
Where is the text u have read from file?Is it stored in some file? while iterating where are you using 'i' ?
To update this, HTML 5 File Upload it is supported in IE 10 and above .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.