无法让meteor FileCollection包工作

时间:2014-05-16 05:03:04

标签: events meteor packages resumablejs

我(不幸的是)在Windows机器上工作,所以我不得不手动将FileCollection包添加到我的应用程序,但是当我运行我的应用程序时,我可以从浏览器控制台访问文件集合和文件收集方法。但是,我似乎无法在实际页面上设置事件侦听器。 (仅供参考,我正在使用铁路由器进行模板化架构。)

似乎需要调用的代码并没有按照正确的顺序进行,但我已经尝试了放置代码的位置,似乎没有任何区别。

服务器端代码:

// Create a file collection, and enable file upload and download using HTTP
Images = new fileCollection('images',
  { resumable: true,   // Enable built-in resumable.js upload support
    http: [
      { method: 'get',
        path: '/:_id',  // this will be at route "/gridfs/images/:_id"
        lookup: function (params, query) {  // uses express style url params
          return { _id: params._id };       // a mongo query mapping url to myFiles
        }
      }
    ]
  }
);

if (Meteor.isServer) {

  // Only publish files owned by this userId, and ignore
  // file chunks being used by Resumable.js for current uploads
  Meteor.publish('myImages',
    function () {
      return Images.find({ 'metadata._Resumable': { $exists: false },
                   'metadata.owner': this.userId });
    }
  );

  // Allow rules for security. Should look familiar!
  // Without these, no file writes would be allowed
  Images.allow({
    remove: function (userId, file) {
      // Only owners can delete
      if (userId !== file.metadata.owner) {
        return false;
      } else {
        return true;
      }
    },
    // Client file document updates are all denied, implement Methods for that
    // This rule secures the HTTP REST interfaces' PUT/POST
    update: function (userId, file, fields) {
      // Only owners can upload file data
      if (userId !== file.metadata.owner) {
      return false;
      } else {
        return true;
      }
    },
    insert: function (userId, file) {
      // Assign the proper owner when a file is created
      file.metadata = file.metadata || {};
      file.metadata.owner = userId;
      return true;
    }
  });
}

客户端代码(当前位于客户端目录顶层的main.js中):

if (Meteor.isClient) {
    // This assigns a file upload drop zone to some DOM node
Images.resumable.assignDrop($(".fileDrop"));

// This assigns a browse action to a DOM node
Images.resumable.assignBrowse($(".fileBrowse"));

// When a file is added via drag and drop
Images.resumable.on('fileAdded', function(file) {
// Create a new file in the file collection to upload
    Images.insert({
  _id : file.uniqueIdentifier, // This is the ID resumable will use
      filename : file.fileName,
      contentType : file.file.type
      }, function(err, _id) {// Callback to .insert
        if (err) {throwError('Image upload failed');}
    // Once the file exists on the server, start uploading
        Images.resumable.upload();
});
  });
  Images.resumable.on('fileSuccess', function(file) {
var userId = Meteor.userId();
var url = '/gridfs/images/' + file._id;
Meteor.users.update(userId, {
  $set : {
    "profile.$.imageURL" : url
  }
    });
Session.set('uploading', false);
  });
  Images.resumable.on('fileProgress', function(file) {
Session.set('uploading', true);
  });
}

1 个答案:

答案 0 :(得分:0)

我认为问题可能在于使用IronRouter。我假设您正在使用一些layout.html通过Iron路由器,并在其中添加了您的文件表格模板以供显示。 (我猜你的是跟随fileCollection附带的sampleApp。)。当我这样做时,我遇到了一个问题,这与我附加监听器的代码的位置有关。问题是你有代码“Images.resumable.assignDrop($(”。fileDrop“));”在您的客户端文件中。现在的方式是,在layout.html中呈现模板之前,该行代码正在运行。所以代码找不到DOM元素“.fileDrop”。要修复此问题,请创建一个layout.js文件并使用这样的渲染方法...

Template.layout.rendered = function(){
    Images.resumable.assignDrop($(".fileDrop"));
}