html5 文件系统(二)

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



猜你喜欢

转载自blog.csdn.net/myliveisend/article/details/19154367