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
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 /> 3 Comments
svachalek
I don't know if things have changed since this was written but I see results in event.target.result rather than event.result.
ALBI
Where is the text u have read from file?Is it stored in some file? while iterating where are you using 'i' ?
Phil
To update this, HTML 5 File Upload it is supported in IE 10 and above .