2014-12-07 91 views
0

你好,我是流星新人,尝试开发一个简单的文件/图片上传/下载。流星无法上传图片

我的代码和包:

meteor create testApp 
meteor add cfs:standard-packages 
meteor add cfs:filesystem 

testApp.js

YourFileCollection = new FS.Collection("yourFileCollection", { 
      stores: [new FS.Store.FileSystem("yourFileCollection", {path: "~/meteor_uploads"})] 
     }); 
if (Meteor.isClient) { 
     // counter starts at 0 
    Template.yourTemplate.events({ 
     'change .your-upload-class': function(event, template) { 
      FS.Utility.eachFile(event, function(file) { 
       var yourFile = new FS.File(file); 
       yourFile.creatorId = Meteor.userId(); // add custom data 
       YourFileCollection.insert(yourFile, function (err, fileObj) { 
        if (!err) { 
         // do callback stuff 
        } 
       }); 
      }); 
     } 
    }); 
    } 

    if (Meteor.isServer) { 
     YourFileCollection.allow({ 
      insert: function (userId, doc) { 
       return !!userId; 
      }, 
      update: function (userId, doc) { 
       return doc.creatorId == userId 
      }, 
      download: function (userId, doc) { 
       return doc.creatorId == userId 
      } 
     }); 
    } 

和HTML:

<head> 
    <title>protoSonn</title> 
</head> 

<body> 
    <h1>Welcome to Meteor!</h1> 

    {{> yourTemplate}} 
</body> 

<template name="yourTemplate"> 
    <input class="your-upload-class" type="file"> 
</template> 

在我的/ testApp /公用文件夹也两幅图像。 在我的公用文件夹中选择一个图像后,单击确定。没有事情发生。在MongoDB中没有创建任何集合。怎么了?

回答

0

首先,我看你是不是使用账户包验证和你是不必要的,包括Meteor.userId()为您收集和第二已允许/拒绝规则,这是行不通的obvivious由于未列入账目包

第一/简单的解决方案:从代码

yourFile.creatorId = Meteor.userId(); // add custom data 

删除下面的行和允许/拒绝规则

if (Meteor.isServer) { 
     YourFileCollection.allow({ 
      insert: function (userId, doc) { 
       return !!userId; 
      }, 
      update: function (userId, doc) { 
       return doc.creatorId == userId 
      }, 
      download: function (userId, doc) { 
       return doc.creatorId == userId 
      } 
     }); 
    } 

这里是在流星垫工作代码:http://bit.ly/1u8gpOq
上传后,在开发者控制台检查文件具有以下命令 -

YourFileCollection.find().fetch() 

解决方法二:

包括认证到您的应用帐户包等等,你会得到Meteor.userId()一些值,然后显示上传屏幕