2014-05-16 92 views
0

我(不幸)在Windows机器上工作,所以我不得不手动添加FileCollection包到我的应用程序,但是当我运行我的应用程序时,我可以访问文件集合和文件从浏览器控制台收集方法。但是,我似乎无法让事件监听器在实际页面上设置。 (仅供参考,我为我的模板架构使用铁路由器。)不能让流星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); 
    }); 
} 

回答

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")); 
} 
+0

谢谢!这就是我错过了让代码以正确的顺序执行的问题。不幸的是,我似乎已经搞砸了其他的东西,因为我只能成功运行一次。而且,我似乎无法复制我所做的。有关如何处理sampleApp的其他代码的想法?将它保存在名为'collections'的顶层文件夹中的文件是否可以吗? – esteban3643345

+0

发现我的(最新)问题。它与fileCollection无关。你的回答明确地解决了这个问题。 – esteban3643345