【问题标题】:Meteor: how to upload a file and then unzip it using fs file system?Meteor:如何上传文件,然后使用 fs 文件系统解压缩?
【发布时间】:2015-12-02 08:49:53
【问题描述】:

如果我们使用 CFS for meteor 在线上传大文件,并且当存储(不上传)完成时我们必须触发另一个事件,例如解压缩文件,那么我们将如何做呢? isUpload 在这种情况下不合适,因为文件尚未存储,因此无法解压缩。写在客户端做什么。

JavaScript:

var Zipper = new FS.Collection("zip", {
  stores: [new FS.Store.FileSystem("zip")]
});

if (Meteor.isClient) {

  Template.upload.helpers({
    'data':function(){
      return Zipper.find(); 
    }
  });

  Template.upload.events({
    'change #fileUpload': function (event,template) {
      FS.Utility.eachFile(event,function(file) {
        var fileObj = new FS.File(file);
        var fileId;
        Zipper.insert(fileObj,function(err){
         });
      })
    }
  });
}

if (Meteor.isServer) {
  Meteor.methods({
    "unZipFile":function(fileId, fileName){
       //unzip the file using node package
    }
  });

}

HTML:

<head>
  <title>uploadAndZip</title>
</head>

<body>
    <div class="container">
        <h1>Welcome to Meteor!</h1>
         {{> upload}}
    </div>


</body>

<template name="upload">
    <input type="file" id="fileUpload">  
    {{#each data}}
      {{#unless this.isUploaded}}
      {{> FS.UploadProgressBar bootstrap=true}}
      {{/unless}}
    {{/each}}
    <table class="table table-striped table-hover ">
        <thead>
            <tr>
                <th>Name</th>
                <th>Size</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>
            {{#each data}}
                <tr class="success">
                    <td>{{name}}</td>
                    <td>{{size}} bytes</td>
                    <td>{{updatedAt}}</td>
                </tr>
            {{/each}}
        </tbody>
    </table>
</template>

我尝试向This post 寻求帮助,但没有帮助。

【问题讨论】:

  • 为什么不直接使用 transformWrite? github.com/CollectionFS/…
  • 我用过。我正在使用的解压缩包(decompress-zip)给出错误 [错误:偏移量超出范围]

标签: javascript node.js file-upload meteor zip


【解决方案1】:

我使用 nodejs 解压缩和打包解决了类似的问题:meteorhacks:npm。 使用on stored不需要方法,上传zip时会执行函数。

在这种情况下,我只提取 tif(f) 和 jpeg。

Zips.on('stored',  Meteor.bindEnvironment(function (fileObj) {

    var unzip = Meteor.npmRequire('unzip');
    var fs = Meteor.npmRequire('fs');
    fs.createReadStream(Meteor.settings.public.zipFolder+fileObj.copies.zip.key)
        .pipe(unzip.Parse())
        .on('entry', function (entry) {
            var fileName = entry.path;
            var type = entry.type; // 'Directory' or 'File'
            var size = entry.size;
            if (fileName.match(/(.tif+|.jpg|.jpeg)$/)) {
                console.log("extracting file: "+fileName);
                entry.pipe(fs.createWriteStream('/tmp/'+fileName));
            } else {
                entry.autodrain();
            }
        });
}));

【讨论】:

    猜你喜欢
    • 2011-05-02
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多