Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSZip: Get content of file in zip from file input

I want to get the content of a zip from input with JSZip. I can read the title of my file but how do I get the content

I tried with 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'));
 })
});

return: [object Promise]

What can I do to get the plain text

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

THX!

like image 639
howtoweb Avatar asked Dec 25 '22 00:12

howtoweb


1 Answers

async, like loadAsync returns a Promise. Here you can chain them:

$('.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);
 });
});
like image 112
David Duponchel Avatar answered Dec 31 '22 12:12

David Duponchel