views:

213

answers:

3

Is it possible to read the content of a file that has been selected with a file input control? I wish to do something like:

<input type="file" id="fileInput" onblur="readFile(this)"/>

<script language="javascript">
   function readFile(file) {
     document.write(file);
   }
</script>

Is anything like this possible? or does the file upload just send the file to the server.

+2  A: 

It is possible in Firefox, but it is not standardized, so it is not possible portably across browsers (for instance, WebKit does not support it). Your best bet would probably be to upload the file to the server, and then download it again using an XMLHTTPRequest.

Brian Campbell
this is a good suggestion, unfortunately, I need to be able to run completely on the client side. This app may be distributed on a CD.
alumb
In that case, you're out of luck unless you can guarantee your users will be using Firefox.
Brian Campbell
nope, 95% will be IE (probably 6) and the rest are unknown. So it needs to be cross browser.
alumb
Then you should probably include a FF3 install file on the CD. Otherwise, like Brian says, you're out of luck.
Calvin
BTW, what kind of user demographic would be 95% IE6? IE7 has come with Windows XP since SP3 and has 25% market share to IE6's 17%. Firefox in comparison has 46%.Is this a Korean application by any chance? (I heard that IE is deeply entrenched in South Korea because everyone uses ActiveX components on their sites)
Calvin
no. Oil and Gas industry. Our firm is still set on IE6 (for reason I'm not sure of (read "crazy IT department")) and we have to be able to deploy internally as well as to clients.
alumb
@Calvin HealthCare .... it's a shame that everyone can't share in the IE6 blues
Chad Grant
A: 

It is probably not possible in many browsers. What would happen if we gave arbitraty javascript the ability to read an arbitrary file in the filesystem, using the user's credentials? BAD THINGS. Malicious javascript could easily take the file data and post it back to the server, quietly snooping all your files in the background.

I doubt this is possible, and I strongly recommend against it.

If it needs to be exclusively client-side, why are you using a web application at all? The only files this could display are plain-text, for which there are many easier ways of viewing content.

JJO
He did not ask about arbitrary access to arbitrary files. He asked about whether you could read from a file that the user had selected for upload using a fileinput control; exactly the same way you would select a file for uploading to the server.
Brian Campbell
+1  A: 

You can if you use HTA (Hypertext Terminal Application, see http://msdn.microsoft.com/en-us/library/ms536496(VS.85).aspx). If you do, you're bound to Internet Explorer, but free to access files, the registry etc. There are (of course) security issues.

KooiInc