Modern browsers that implement FileReader can do this. To check your browser, check if window.FileReader .
Here is the code I wrote just this morning to do just that. In my case, I just drag and drop the file onto the HTML element called panel.in1 here, but you can also use <input type="file" /> (see link below).
if (window.FileReader) { function dragEvent (ev) { ev.stopPropagation (); ev.preventDefault (); if (ev.type == 'drop') { var reader = new FileReader (); reader.onloadend = function (ev) { panel.in1.value += this.result; }; reader.readAsText (ev.dataTransfer.files[0]); } } panel.in1.addEventListener ('dragenter', dragEvent, false); panel.in1.addEventListener ('dragover', dragEvent, false); panel.in1.addEventListener ('drop', dragEvent, false); }
This is the reader.onloadend function, which receives the text of the file that you are restoring in the event handler, like this.result .
I got most of the mechanism on how to do this from MDN: https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications
source share