Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Save a file at client side using JavaScript?

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.

like image 558
Amit Avatar asked Nov 10 '11 14:11

Amit


People also ask

Can JavaScript write to file?

There is a built-in Module or in-built library in NodeJs which handles all the writing operations called fs (File-System). It is basically a JavaScript program (fs. js) where function for writing operations is written. Import fs-module in the program and use functions to write text to files in the system.


1 Answers

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

like image 69
Quentin Avatar answered Oct 12 '22 23:10

Quentin