admin管理员组文章数量:1391929
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 badges1 Answer
Reset to default 8Matti 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 />
本文标签: Javascript read a text file being uploaded through a formStack Overflow
版权声明:本文标题:Javascript read a text file being uploaded through a form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744746768a2622929.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论