Javascript read a text file being uploaded through a form - Stack Overflow

I want to do some basic calculations based on a text file. However since it's for a client it need

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?

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?

Share Improve this question asked Dec 6, 2010 at 6:32 ChrisChris 6411 gold badge9 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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 />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744746768a4591347.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信