Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  5] [ 2]  / answers: 1 / hits: 47660  / 11 Years ago, thu, may 2, 2013, 12:00:00

I read few older thread about the same, but seen the file API changed a lot recently. My requirement is to save a json file (data is locally in indexdDB, but I need a way to back it up). Since I use indexdDB, I only target recent browsers, mainly chrome. So, it it possible to save data (json string) to client computer?



I have seen http://eligrey.com/demos/FileSaver.js/ , but is there a way to do it natively?



Thanks.


More From » html

 Answers
7

You can use a Blob and the HTML5 a[download] feature to provide a JSON backup download:



var data = {a:1, b:2, c:3};
var json = JSON.stringify(data);
var blob = new Blob([json], {type: application/json});
var url = URL.createObjectURL(blob);

var a = document.createElement('a');
a.download = backup.json;
a.href = url;
a.textContent = Download backup.json;


Here is a jsfiddle example: http://jsfiddle.net/potatosalad/yuM2N/


[#78482] Tuesday, April 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;