将json字符串保存到客户端pc(使用HTML5 API) [英] Save json string to client pc (using HTML5 API)

查看:118
本文介绍了将json字符串保存到客户端pc(使用HTML5 API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了几篇关于相同的旧线程,但最近看到API文件改变了很多。我的要求是保存一个json文件(数据在indexdDB本地,但我需要一种方法来备份它)。由于我使用的是indexdDB,因此我只针对最近的浏览器,主要是Chrome浏览器。那么,是否可以将数据(json字符串)保存到客户端计算机?

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?

我见过 http://eligrey.com/demos/FileSaver .js / ,但有没有办法原生呢?

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

谢谢。

推荐答案

您可以使用 Blob 和HTML5 [下载] 功能,提供JSON备份下载:

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";

以下是一个jsfiddle示例: http://jsfiddle.net/potatosalad/yuM2N/

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

这篇关于将json字符串保存到客户端pc(使用HTML5 API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆