0

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

似乎需要调用的代码没有按正确的顺序出现,但我已经尝试过放置代码的位置,但似乎没有任何区别。

服务器端代码:

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

1 回答 1

0

我认为问题可能与使用 IronRouter 有关。我假设您正在通过 Iron 路由器使用一些 layout.html,并且在其中您已经添加了要显示的文件表的模板。(我猜你正在关注 fileCollection 附带的 sampleApp。)。当我这样做时遇到了一个问题,这与我在哪里附加了监听器的代码有关。问题是你有代码“Images.resumable.assignDrop($(".fileDrop"));” 在您的客户文件中。按照您现在的方式,该行代码在您的模板在 layout.html 中呈现之前运行。所以代码找不到DOM元素“.fileDrop”。要解决此问题,请创建一个 layout.js 文件并使用如下呈现的方法...

Template.layout.rendered = function(){
    Images.resumable.assignDrop($(".fileDrop"));
} 
于 2014-05-19T14:28:27.120 回答