4

Hi is it possible to save json at client side? For example see below code, obj object is modified on each click and I want to save this modified object as json at user machine, which user can load back later on.

I want this specific solution,as the data I am going to save will be huge. Cookies(as it will get lost if user cleans browser) and HTML5 local storage is not an option(due to 2.5MB to 10MB, varying with browser). The data I am going to save will be base64 images. If user saves 50 or more images, 10 MB is a small size.

This question is not duplicate, at least not of Maximum size limits of local storage. As data requirement in my problem is already surpassing, max local storage of various browser. Moreover this local storage various with browser, so the amount of data user can save will be varying across browser, which is not desired.

var obj=[{name:"Some Init data"}];


/*
this function adds new data based on html form fields to this obj object
*/
function someFunctionOnClick(){
    obj.push({name:"some other data from some other html field"});
}

/*
Want to save this object: obj object as json on client machine,
so that he can use my app even offline, just by loading JSON back stored
locally on his machine
    */
function saveThisJSONLocallyOnClientSystem(){
    var data=JSON.stringify(obj);
}
danwellman
  • 9,068
  • 8
  • 60
  • 88
user3769778
  • 967
  • 2
  • 7
  • 26
  • 2
    why is localstorage not an option? – gurvinder372 Oct 11 '16 at 09:04
  • You can use the cookie,then save the JSON string,and if you want to use the JSON,just use `JSON.parse` – Felix Fong Oct 11 '16 at 09:05
  • @gurvinder372 It varies with Browser, ranging from 2.5 MB to 10MB: http://stackoverflow.com/questions/2989284/what-is-the-max-size-of-localstorage-values and more over , I had image files too to save at user machine , which I am saving as base64. It will just surpass this 10 MB cap. – user3769778 Oct 11 '16 at 09:05

1 Answers1

3

IndexedDB allows client-side storage of huge amounts of data, much more than localStorage.

Browser support for indexedDB is quite good, the following excerpt is from Wikipedia:

Preliminary support for IndexedDB is included by Firefox (since version 4), Google Chrome (since version 11), and by Internet Explorer 10 Consumer Preview and Metro style apps. Apple announced support in Safari 8 for both iOS 8 and OS X at their WWDC 2014 Keynote on June 2, 2014.

there is also a polyfill for older browsers

danwellman
  • 9,068
  • 8
  • 60
  • 88