I am trying to provide a script - one solution for reading the contents of a file on a client machine through a browser.
I have a solution that works with Firefox and Internet Explorer. This is ugly, but I'm just trying:
function getFileContents() { var fileForUpload = document.forms[0].fileForUpload; var fileName = fileForUpload.value; if (fileForUpload.files) { var fileContents = fileForUpload.files.item(0).getAsBinary(); document.forms[0].fileContents.innerHTML = fileContents; } else { // try the IE method var fileContents = ieReadFile(fileName); document.forms[0].fileContents.innerHTML = fileContents; } } function ieReadFile(filename) { try { var fso = new ActiveXObject("Scripting.FileSystemObject"); var fh = fso.OpenTextFile(filename, 1); var contents = fh.ReadAll(); fh.Close(); return contents; } catch (Exception) { return "Cannot open file :("; } }
I can call getFileContents() and it will write the contents to the text area of fileContents .
Is there any way to do this in other browsers?
At the moment, I'm most interested in Safari and Chrome, but I'm open to suggestions for any other browser.
Edit: In response to the question: “Why do you want to do this?”:
Basically, I want to hash the contents of the file along with a one-time password on the client side so that I can send this information as confirmation.
javascript html file-io sandbox
Damovisa Apr 15 '09 at 2:06 2009-04-15 02:06
source share