2

I am taking a text file from user and then posting that file back to the browser using ajax storing the content in db and then showing the content back to user page using Jquery post response. Now i want to something like this..

Read the text file from the user computer using javascript. Display the content and when he submits the page I will save the values.

0

4 Answers 4

4

Can't be done in pure JS for security reasons. You would need to have the user upload the file to your server, and fetch the contents back through Ajax.

If you use Flash or Java, you should be able to gain direct access to the file. If you speak Flash/Actionsript, maybe SWFUpload's source code (especially the new client-side resizing functions) can serve as an inspiration.

Update: This blog entry should help. Read and write local files with Flash Player 10

Update: To elaborate on the "upload and fetch" thing, if you do the uploading in an IFRAME, you could even have the upload script simply output the text file's contents. Because the iframe belongs to your domain, you will be able to retrieve its contents via JavaScript when the upload has finished. As long as you send a content-type: application/text it should be fairly safe from any malicious attacks.

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

2 Comments

You can, but for now it's only supported by Firefox 3.6+. Link: developer.mozilla.org/en/Using_files_from_web_applications
@EarthMind Interesting, that would be worth an answer in itself IMO! HTML 5 is not that far away any more, at least when you don't need to support every ancient browser.
2

If you're ok with Firefox 3.6 support only check out https://developer.mozilla.org/en/Using_files_from_web_applications, otherwise you should use Flash, Java or silverlight for this.

1 Comment

The input.files array part of this also works in Firefox 3.5.
0

You won't be able to read a file in user's computer due to security issues.

Comments

0

Reading client files in javascript is possible with the new File API available in modern browsers. Check this site and its code: http://www.readfileonline.com/

However, before reading file contents in javascript, the user must explicitly select the files it allows to read. This is a security feature of the standard.

Comments

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.