JSZip:从文件输入中获取zip文件的内容

时间:2016-10-24 16:31:24

标签: javascript jquery html html5 jszip

我想通过JSZip从输入中获取zip的内容。 我可以阅读文件的标题,但如何获取内容

我尝试使用jQuery:

$('.upload-input').on('change', function($event) {
 var $file = $event.target.files[0];
 JSZip.loadAsync($file).then(function($content) {
  alert($content.files["css/style.css"].async('text'));
 })
});

返回:[对象承诺]

我该怎么做才能获得纯文本

JSFiddle:https://jsfiddle.net/jyuy7q6j/

THX!

1 个答案:

答案 0 :(得分:7)

async一样,

loadAsync会返回Promise。在这里你可以链接它们:

$('.upload-input').on('change', function($event) {
 var $file = $event.target.files[0];
 JSZip.loadAsync($file).then(function($content) {
  // if you return a promise in a "then", you will chain the two promises
  return $content.files["css/style.css"].async('text');
 }).then(function (txt) {
   alert(txt);
 });
});