html5 文件系统(二)

来源:互联网 发布:centos vim如何使用 编辑:程序博客网 时间:2024/06/17 05:05
js生成BLob然后下载该Blob。
现在页面又这样一个json数据
var data = { age: 42, message: "hello, world", date: new Date() }

然后我想直接让用户下载,下次提交给服务器。

方法如下:
var saveData = (function () {    var a = document.createElement("a");    document.body.appendChild(a);    a.style = "display: none";    return function (data, fileName) {        var json = JSON.stringify(data),            blob = new Blob([json], {type: "octet/stream"}),            url = window.URL.createObjectURL(blob);        a.href = url;        a.download = fileName;        a.click();        window.URL.revokeObjectURL(url);    };}());

最后调用saveData方法。

saveData(data, "myfile.txt");



0 0