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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
In that case, you're out of luck unless you can guarantee your users will be using Firefox.
nope, 95% will be IE (probably 6) and the rest are unknown. So it needs to be cross browser.
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)
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.
|
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.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.