Reputation: 1142
Lets say, I have a simple HTML page with
<input id="input-file" type="file" />
this input field is NOT part of any form. User chooses a file from file system for this input.
How can I load contents of file chosen by user to a JavaScript variable (e.g. as a string or somehow compressed) when some BUTTON is pressed?
I can use only pure javascript or dojo/dojox.
If this is impossible or hard to do, the input can be part of a form. Nevertheless the goal still is to save the contents of a file to a javascript variable.
The solution should be well supported even by the browsers that are not the newest (2014).
Upvotes: 0
Views: 37
Reputation: 2068
You could use the File API which is getting support in some of the newer browsers. Read more about it on this SO-thread which deals with the same problem you are describing: https://stackoverflow.com/a/754398/844932.
EDIT: According to this link, http://caniuse.com/#feat=filereader, the FileReader API is fairly well supported today (IE10+ and all other relevant browsers), so I think you'll probably be well off using it.
Upvotes: 1