64

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

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
menardmam
  • 9,860
  • 28
  • 85
  • 113
  • 2
    Can you not write the answer to a field within the HTML? – Darryl Hein Feb 24 '09 at 18:16
  • That 's not true anymore. but the access you can get through javascript are restricted to what the user allow you. – Kiwy Dec 16 '13 at 09: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. – Evan Plaice Jan 21 '16 at 15:02

5 Answers5

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.

DJDaveMark
  • 2,669
  • 23
  • 35
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 9
    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: http://diveintohtml5.org/storage.html – Chris Jaynes Nov 10 '10 at 16:43
  • 3
    @ChrisJaynes - that link is broken, I think you want this one: [http://diveintohtml5.info/storage.html](http://diveintohtml5.info/storage.html) – rjohnston Jun 07 '12 at 10:11
  • @marcgg I think you may have confused Java (made by Sun) with JavaScript. – Justin Putney May 02 '13 at 18:32
  • @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. – Justin Putney Dec 02 '13 at 21:30
  • @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 :) – marcgg Dec 03 '13 at 07:52
  • A use case where you'd want to have stuff inside a txt file. A data app where there's a "DOWNLOAD AS TEXT" button for the data. – GeorgeWL Dec 05 '17 at 09:40
16

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

Javier
  • 60,510
  • 8
  • 78
  • 126
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.

jonstjohn
  • 59,650
  • 8
  • 43
  • 55
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.

Clyde
  • 8,017
  • 11
  • 56
  • 87
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 :)

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227