简体   繁体   中英

JSZip won't load a zip from a base64 string?

So I'm trying to write a small JSON object to the URL as a user changes items on a page, and also allow the URL to be read to let the user pick up where they left off.

I'm able to generate the zip fine with JSZip, but I'm unable to figure out how to later open the zip from that string. Here's the code I was working with. zip.file has elements, but I don't know how to read the base64 string back to zip2 to be able to open it.

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)

JSZip doesn't use all attributes on its instance, you need to use the file() method :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM