最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSZip: Get content of file in zip from file input - Stack Overflow

programmeradmin4浏览0评论

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: /

THX!

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/jyuy7q6j/

THX!

Share Improve this question asked Oct 24, 2016 at 16:31 howtowebhowtoweb 3591 gold badge3 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 15

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

I forgot about files[...].async, so here's my solution (with a little bit of traversing thru the file):

var worldChars = Array.from(zip.files["world.txt"]._data.pressedContent)
var world = ""
for (var ch of worldChars) {
    world += String.fromCharCode(ch)
}
发布评论

评论列表(0)

  1. 暂无评论