5

I want to save a JSON object to a file at client to persist it for future usage, I have tried following line

window.open("data:text/json;charset=utf-8," + escape(JSON.stringify(obj)));

and it works!! Problem it asks user the location and name of the file for saving. What I want is I want the user completely unaware of the fact that something is being saved for future use or atleast keep it to minimum possible user clicks.

How can I give the file name and location statically in window.open() ?

Thanks in advance,

EDIT

Just to make it clear that " I don't store arbitrary or unwanted data. All the users are registered users of the system." In normal conditions I don't store anything locally. However I want to store some JSON objects if the network was not available at the time of form submission.

One obvious solution will be to use cookies. Since Cookies can be accidentally deleted due to user's browser settings. I need a way to persist the data till the network becomes available. It will be greater to have cross browser support.

Amit
  • 13,134
  • 17
  • 77
  • 148
  • 4
    Java is not JavaScript. They are completely separate languages. – Pointy Nov 10 '11 at 14:22
  • Also, you can't just force files to be saved on client machines from a browser environment. It's a fairly obvious security measure. – Pointy Nov 10 '11 at 14:22
  • @Kapep I want the values for cross browser support, even if the user changes the browser he/she should be able to proceed from where he left. I am storing information in JSON objects. – Amit Nov 10 '11 at 14:24
  • 2
    The correct MIME-Type for JSON is `application/json` ;) – Quasdunk Nov 10 '11 at 14:25
  • @Pointy Of course I know that and I think there are some ways to tweak this... I can ask user for permission if there is no other way, As I said earlier I already have a working code, I just want to minimize the user clicks.. – Amit Nov 10 '11 at 14:26
  • @Quasdunk +1 for pointing the correct MIME-Type – Amit Nov 10 '11 at 14:27
  • 2
    What you're suggesting would pose a major security risk. Not to mention annoyance, with thousands of files piling up everywhere on your hard drive from every page you visit on the internet. And how would you go about writing to C:\Windows\System32\ on anything other than a Windows machine? – flesk Nov 10 '11 at 14:29

6 Answers6

7

If you want to persist data, then use a storage API, you can't play games with the user's filesystem.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    +1, also try [store.js](https://github.com/marcuswestin/store.js) - a localStorage wrapper. – Daniel Nov 10 '11 at 14:27
4

You can't do that, it is a question of security on client side.

Gfox
  • 307
  • 1
  • 6
  • It is possible to save files on the client-side [using service workers](https://developers.google.com/web/ilt/pwa/caching-files-with-service-worker), which didn't exist when this question was written. – Anderson Green Mar 19 '22 at 21:33
2

FSO.js wraps the temporary/persistent FileSystem API and will allow you to easily read/write files... only supported by Chrome right now, but the spec is coming along fast. :)

kwh
  • 364
  • 1
  • 3
  • 7
  • "The file system API that this project is based on has been deprecated, and is no longer supported by most major browsers. This repository exists for archival purposes only." https://github.com/keithwhor/FSO.js/ – Aryeh Armon Dec 12 '16 at 12:42
2

I want the values for cross-browser support, even if the user changes the browser he/she should be able to proceed from where he left. I am storing information in JSON objects.

I think users don't expect a web application to share information with other browsers on the same machine this way. Also, I doubt many users change their browser too frequently anyway to warrant a privacy-invasive feature like this. You should either consider storing the information on your server (by forcing a user to register or using common accounts like OpenID, Google, Facebook etc.) or on the client side by setting a cookie or using the mentioned storage technologies.

If you really want to restrict stored information to browsers on the same machine, and don't want to permit access by the same user on different machines, you could take a look into LSO ('Flash cookies') which seem to be saved browser independent. You don't need any user confirmation for storing LSOs.

Update: Flash isn't supported by browsers as it used to be and not widely used nowadays, so LSOs aren't a good option any more.

kapex
  • 28,903
  • 6
  • 107
  • 121
  • users will be already a registered user of my site... and I store data only when there is temporarily no network is awailable.. for all other cases (i.e. when the network is available) I dont store anything.... anyways Thanks.. – Amit Nov 11 '11 at 08:15
2

FileSystem APIs is part of HTML5 spec and it is possible to access file system in a sandbox for a certain website in modern browsers, here is a good tutorial: http://www.html5rocks.com/en/tutorials/file/filesystem/

However I would go with LocalStorage API for that matter which has better browser support: http://www.w3schools.com/html5/html5_webstorage.asp

Kamyar Nazeri
  • 25,786
  • 15
  • 50
  • 87
  • "The file system API that this project is based on has been deprecated, and is no longer supported by most major browsers. This repository exists for archival purposes only." https://github.com/keithwhor/FSO.js/ – Aryeh Armon Dec 12 '16 at 12:43
1

As has already been mentioned in the comments, this is not possible without express user consent for obvious security reasons. however, as also mentioned in comments, you shoudl/could store the information in a cookie which will then be retrievable when the user returns.

something like this would do it

document.cookie="cookie_name="+required_value+"; expires=Monday, 04-Dec-2011 05:00:00 GMT";

obviously the expires time will be as long as you need the information to persist.

hope that helps

olly_uk
  • 11,559
  • 3
  • 39
  • 45
  • I have the value in cookie.. actually it is the cookie which I am writing (or trying to write) in a file for the case where user changes the browser next time or clears the cookies... – Amit Nov 10 '11 at 14:31