Is it possible to intercept a file from <input type = file> in javascript?

Is it possible to read the contents of a file that was selected using file entry control? I want to do something like:

<input type="file" id="fileInput" onblur="readFile(this)"/> <script language="javascript"> function readFile(file) { document.write(file); } </script> 

Is this possible? or uploading a file simply sends the file to the server.

+2
javascript file-upload
source share
3 answers

It is possible in Firefox , but it is not standardized, so it is not possible in browsers (for example, WebKit does not support it). The best thing would be to upload the file to the server and then upload it again using XMLHTTPRequest .

+2
source share

You can use HTA (hypertext terminal application, see http://msdn.microsoft.com/en-us/library/ms536496(VS.85).aspx ). If you do, you are tied to Internet Explorer, but you can access files, registry, etc. There are (of course) security issues.

+1
source share

This is probably not possible in many browsers. What happens if we provide javascript for arbitration with the ability to read an arbitrary file in the file system using user credentials? BAD THINGS. Malicious javascript can easily take file data and send it back to the server, quietly tracking all your files in the background.

I doubt it is possible, and I highly recommend against it.

If this should be exclusively client-side, why are you using a web application? The only files that can be displayed are plain text, for which there are many simpler ways to view the contents.

0
source share

All Articles