浏览器中javascript简易实现json数据保存到客户端

  思路很简单,就是利用Blob、URL.createObjectURL()方法和<a> 便签的HTML5新属性download来模拟远端文件下载保存。下面直接上代码

savePath: function(){
      var me = this;
      if(!me.curve){
        $.uitools.alertDlg("Path还没有定义,请先定义路径!");
        return;
      }
      var json = me.pathToJson();
      var blob = new Blob([json]);
      var url =  URL.createObjectURL(blob);
      var a = document.createElement("a");
      a.setAttribute('download','path.json');
      a.setAttribute('href',url);
      document.body.appendChild(a);
      a.click();
      a.remove();
    },

 

posted @ 2022-10-09 13:30  柒零壹  阅读(132)  评论(0编辑  收藏  举报