Choose & read file in PHP
In a form in PHP, I have a textarea and a Choose File option. The code for Choose File is 开发者_Python百科as below
<input name="upload_file" type="file" id="Browse" title="Browse" value="Browse" />
What I have to do is Choose a file and display its contents in the textarea, as soon as I select the file (No click event to happen).
How can I go about it? I am not very sure of how I can get a handle of the file object?
This is impossible using a file
upload: You won't have "live" access to the file through JavaScript.
You would have to actually upload the file. You could then request its contents back in an Ajax request (or upload the file into a hidden iframe, output its contents there and grab them through JavaScript).
Flash can access files on the client's computer directly. Uploaders like SWFUpload use this to resize images on client side. If you're versed in Flash, it should be fairly easy to put something together.
you can't until the file is uploaded to/received by the server
You'll need to use an onChange event of the input field to trigger a submit (or an ajax request that uploads the file)... then PHP can read the file and rebuild the page (or send a response to the ajax request) to include it's content in the textarea
An example of how to do this
If you simply wish to display the file contents on the client side, before uploading to the server, you will need to implement Javascript and a Java Applet or ActiveX control (for security reasons).
The following pages may or may not be useful to you for more information:
http://timstall.dotnetdevelopersjournal.com/using_javascript_to_read_a_clientside_file.htm
http://www.html5rocks.com/tutorials/file/dndfiles/
精彩评论