65

Is there a way to get jQuery to get information to and from a file? Is it possible? How?

3
  • 2
    Can you not write the answer to a field within the HTML? Commented Feb 24, 2009 at 18:16
  • That 's not true anymore. but the access you can get through javascript are restricted to what the user allow you. Commented Dec 16, 2013 at 9:02
  • 1
    The sandbox prevents direct file access for security reasons. You can use the HTML5 File API to allow the user to select files from a file dialog. Commented Jan 21, 2016 at 15:02

5 Answers 5

79

No, JavaScript doesn't have access to writing files as this would be a huge security risk to say the least. If you wanted to get/store information server-side, though, you can certainly make an Ajax call to a PHP/ASP/Python/etc. script that can then get/store the data in the server. If you meant store data on the client machine, this is impossible with JavaScript alone. I suspect Flash/Java may be able to, but I am not sure.

If you are only trying to store a small amount of information for an unreliable period of time regarding a specific user, I think you want Web Storage API or cookies. I am not sure from your question what you are trying to accomplish, though.

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

6 Comments

Cookies would work, of course, but with most modern browsers supporting HTML5 local storage, I would go that route. Here's a nice guide to how it works: diveintohtml5.org/storage.html
@ChrisJaynes - that link is broken, I think you want this one: http://diveintohtml5.info/storage.html
@marcgg I think you may have confused Java (made by Sun) with JavaScript.
@marcgg the URL in the comment above links to a Sun/Oracle page, the creators of Java (no mention of "JavaScript" on the page). If you meant to post a different link, you may want to update that.
@JustinPutney I can't edit a comment so I removed it because it's useless now. The link died, but this was javascript documentation. Because sun created java doesn't mean they can't talk about something else :)
|
16

both HTML5 and Google Gears add local storage capabilities, mainly by an embedded SQLite API.

Comments

9

You will need to handle your file access through web programming language, such as PHP or ASP.net.

To set this up, you would:

  • Create a script that handles the file reading and writing. This should be visible to the browser.

  • Send jQuery ajax requests to that script that either write data or read data. You would need to pass all of your read/write information through the request parameters. You can learn more about this in the jQuery ajax documentation.

Make sure that you sanitize any data that you are storing, since this could potentially be a security risk. However, this is really just standard flat-file data storage, and is not necessarily that unusual.

As Paolo pointed out, there is no way to directly read/write to a file through jQuery or any other type of javascript.

Comments

5

Cookies are your best bet. Look for the jquery cookie plugin.

Cookies are designed for this sort of situation -- you want to keep some information about this client on client side. Just be aware that cookies are passed back and forth on every web request so you can't store large amounts of data in there. But just a simple answer to a question should be fine.

Comments

1

If you want to do this without a bunch of server-side processing within the page, it might be a feasible idea to blow the text value into a hidden field (using PHP). Then you can use jQuery to process the hidden field value.

Whatever floats your boat :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.