How can I read a local file in a webpage via Javascript or JQuery?

Background: (Read before you flame ...)

I want the user to be able to SELECT a text file from the "HTML" input file (on their local device) and click the "Download" button. I don't want the file to be loaded directly, I want the javascript to do some validation, formatting, and then display the formatted results to the user. If the user has several options for what they want to do, depending on the results of validation and formatting. (and yes, this is confirmed on the server side as well.)

This is NOT an AJAX request, it asks the user to select a file and then upload it, using javascript that validates and formats the file along the way.

Question:

Is it possible to read a file (selected by the user for upload) into an HTML webpage where it can be viewed by the user and parsed by Javascript or JQuery? If possible, how?

It should support the major browsers Firefox, Opera, Safari, Chrome and IE8 +.

+3


source to share


2 answers


The closest thing you'll find is the FileReader API , which is currently a draft with limited browser support .



You may need to use a signed Java applet to get broader support (but then you start depending on the people who have the Java plugin installed).

+5


source


There are only two ways I can think of besides unfinished and unsupported technology.



  • You have a two step process where the user uploads it to the server and the server sends it back so you can do any validation, and then when the validation is complete you post it back to the server (not as an upload, you already have the data ).

  • Flash has disk access, you can write a small Flash movie that takes a file and then passes it to javascript, but then you depend on flash.

0


source







All Articles