2016-02-05 75 views
0

所以我試圖寫一個小的JSON對象到URL作爲用戶更改頁面上的項目,並且還允許讀取URL以讓用戶選擇他們離開的地方。JSZip不會從base64字符串加載zip文件?

我能夠用JSZip生成zip,但我無法弄清楚如何以後打開該字符串的zip。這是我正在使用的代碼。 zip.file包含元素,但我不知道如何將base64字符串讀回zip2以便能夠打開它。

var figures = [{ 
    "qty": 1, 
    "name": "", 
    "level": 1, 
    "defense": 1, 
    "melee": 3, 
    "ranged": 1, 
    "abilities": [ 
     "c02","c12","c22","c32","t12" 
    ] 
    },{ 
    "qty": 1, 
    "name": "", 
    "level": 1, 
    "defense": 2, 
    "melee": 1, 
    "ranged": 1, 
    "abilities": [ 
     "c02","c12","c22","c32","t45" 
    ] 
    },{ 
    "qty": 1, 
    "name": "", 
    "level": 4, 
    "defense": 1, 
    "melee": 1, 
    "ranged": 5, 
    "abilities": [ 
     "c01","c14","c23","c35" 
    ] 
    }] 
var zip = new JSZip() 
zip.file = figures 
var urlString = zip.generate({type:"base64"}) 
location.href="#"+ urlString 
console.log(urlString) 
console.log(zip) 
var zip2 = new JSZip() 
zip2.load(urlString,{"base64": true}) 
console.log(zip2) 

回答

0

,您需要使用file()method JSZip不使用它的實例的全部屬性:

var zip = new JSZip(); 
zip.file("figures.json", JSON.stringify(figures)); 
var urlString = zip.generate({type:"base64"}); 

var zip2 = new JSZip(urlString,{"base64": true}); 
console.log(zip2.file("figures.json").asText());