【问题标题】:Meteor Unable to upload an image流星无法上传图片
【发布时间】:2014-12-07 13:04:07
【问题描述】:

您好,我是 Meteor 的新手,尝试开发一个简单的文件/图像上传/下载。

我的代码和包:

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/public 文件夹中还有两张图片。 在我的公用文件夹中选择图像并单击确定后。什么都没有发生。在 mongodb 中没有创建任何集合。怎么了?

【问题讨论】:

    标签: mongodb collections upload meteor


    【解决方案1】:

    首先我看到你没有使用帐户包进行身份验证,你没有必要将Meteor.userId() 包含在你的集合中,其次你有允许/拒绝规则,由于不包含帐户包,这不会很明显

    第一个/简单的解决方案: 从代码中删除以下行

    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() 一些值并在此之后显示上传屏幕

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 2019-05-31
      • 2020-11-27
      • 2023-03-16
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多