瀏覽器中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();
    },

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章