I want to make an "Open File" button to import txt files contents into a textarea.
How can I read the file contents without uploading it to the server?
I want to use javascript (with jquery lib) and I want to do it without refreshing the page.
I want to make an "Open File" button to import txt files contents into a textarea.
How can I read the file contents without uploading it to the server?
I want to use javascript (with jquery lib) and I want to do it without refreshing the page.
This isn't possible with vanilla javascript, you'd need flash or some other approach to do what you want.
The reasoning is security: to prevent you from reading allMyPasswords.txt
:)
I think I will do a simple ajax form to upload the file and then get the content with php before deleting the file and printing it, can't think of another solution.
As you say there's no other way than taking the text on the round trip to the server and back. However to ease the pain a bit you can fully automate this:
target
attribute of the form that holds the file uploadas you set the target to the iframe the response from the server will arrive there, so have your php script send back something like the followin:
echo "<script>window.top.window.file_ready('$file');</script>";
write a js function called file_ready which takes the filename as a parameter and updates the value of your textarea by making an ajax call to the server.
Attribution is due. I learned this technique from AJAX file upload tutorial.