Is there a way to create a text file (and write some JSON data to it ) using client side code only (no server-side code / web services )?
-
1 way I can think of is to store your JSON string into cookie.Raptor– Raptor2014-05-07 04:15:26 +00:00Commented May 7, 2014 at 4:15
-
refer this:stackoverflow.com/questions/16055391/…jhyap– jhyap2014-05-07 04:17:07 +00:00Commented May 7, 2014 at 4:17
-
possible duplicate of how can i create a file on client side by JavaScript?Jason Aller– Jason Aller2014-05-07 04:21:34 +00:00Commented May 7, 2014 at 4:21
-
sure <a download>, downloadify, etc...dandavis– dandavis2014-05-07 06:49:13 +00:00Commented May 7, 2014 at 6:49
3 Answers
You can use local storage to store data client-side, but there is no way to do this server-side.
Comments
My best guess would be that you need to use javascript localstorage to store json.
example :
var myObject = {};
localstorage['myKey'] = myObject;
var secObject = localstorage['mykey'];
//you couls also use :
localstorage.setItem('myKey', myObject);
secObject = localstorge.getItem('myKey');
I usually "stringify" my JSON before saving it in case i need to modify "myObject" after saving it (because when you copy an object you actually copy a reference to it)
localstorage['myKey'] = JSON.stringify(myObject);
secObject = JSON.parse(localstorage['myKey']);
1 Comment
How are you all missing his point? You are able to generate text files and .csv files on client side and deliver it as a download.
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
download("hello.txt","This is the content of my file :)");
From this link: https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server