2

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.

alumb
  • 4,401
  • 8
  • 42
  • 52

3 Answers3

2

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
  • 322,767
  • 57
  • 360
  • 340
  • 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 May 01 '09 at 01:27
  • In that case, you're out of luck unless you can guarantee your users will be using Firefox. – Brian Campbell May 01 '09 at 01:30
  • nope, 95% will be IE (probably 6) and the rest are unknown. So it needs to be cross browser. – alumb May 01 '09 at 01:31
  • Then you should probably include a FF3 install file on the CD. Otherwise, like Brian says, you're out of luck. – Calvin May 01 '09 at 01:35
  • 1
    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 May 01 '09 at 02:07
  • 1
    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 May 01 '09 at 04:16
1

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
  • 119,216
  • 31
  • 141
  • 177
0

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
  • 744
  • 1
  • 6
  • 11
  • 1
    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 May 01 '09 at 04:52