2017-07-19 59 views
0

我没有得到如何正确使用jsZip .. 我想从本地文件夹中获取文件,然后压缩它,最后保存压缩文件在相同的文件夹...保存zip文件夹中的jszip

但我真的没有对如何使用它

这里我做了什么好comprension:

 JSZipUtils.getBinaryContent("http://localhost:8100/pdf/test.pdf", function (err, data) { 
     if(err) { 
      alert('err'); 
      throw err; 
     } 
     else{ 
     zip.file("test.pdf",data,{binary:true}) 
     .then(function (blob) { 
      saveAs(blob, "pdf.zip"); 
     }); 
     } 
    }); 

可以anibody帮助我吗?

回答

0

下面这段代码是我最后工作的东西。

注意,这是jszip utils版本3.它需要filesaver.js来使saveAs调用工作(https://github.com/eligrey/FileSaver.js)。

这里是我的代码:

function create_zip(){ 
    // get checked file urls 
    var urls = Array(); 
    $("input:checked").each(function() { 
     urls.push($(this).siblings('a').attr('href')); 
    }) 
console.log(urls); 

    var zip = new JSZip(); 
    var count = 0; 
    var zipFilename = "archive.zip"; 

    urls.forEach(function(url){ 
     var filename = url.substr(url.lastIndexOf("/")+1); 
console.log(filename); 
     // loading a file and add it in a zip file 
     JSZipUtils.getBinaryContent(url, function (err, data) { 
     if(err) { 
      throw err; // or handle the error 
     } 
     zip.file(filename, data, {binary:true}); 
     count++; 

     if (count == urls.length) { 
      zip.generateAsync({type:'blob'}).then(function(content) { 
       saveAs(content, zipFilename); 
      }); 
     } 
     }); 
    }); 
}; 

这是从这里的信息调整:https://gist.github.com/noelvo/4502eea719f83270c8e9

希望它能帮助!

相关问题