简体   繁体   English

Meteor collectionfs插入服务器端

[英]Meteor collectionfs insert server side

hi everyone i using collectionfs + gridfs + cfs filesystem, on collectionfs documentation i find how to insert file on client side like this : 大家好我使用collectionfs + gridfs + cfs文件系统,在collectionsfs文档中,我发现如何在客户端插入文件,如下所示:

Template.myForm.events({
  'change .myFileInput': function(event, template) {
    FS.Utility.eachFile(event, function(file) {
      Images.insert(file, function (err, fileObj) {
        //Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP
      });
    });
  }
});

on that case will insert file on client side, but in my case i remove insecure, so can't do insert on client side, i try to make it on server side . 在这种情况下将在客户端插入文件,但在我的情况下我删除不安全,所以不能在客户端插入,我尝试在服务器端进行。 so this is my code : 所以这是我的代码:

Template.myForm.events({
    'change . myFileInput': function (event, template) {
        FS.Utility.eachFile(event, function (file) {
            var reader = new FileReader();
            reader.onload = function (fileLoadEvent) {
                Meteor.call('ImageUpload', file, reader.result, function (err, res) {
                    if (err) {
                        console.log(err);
                    } else {
                        alert(res);
                    }
                });
            };
            reader.readAsBinaryString(file);


        });
    }
});

server.js : server.js:

Meteor.methods({
    ImageUpload: function (fileInfo, fileData) {
        console.log(fileInfo);
        Images.insert(fileInfo, fileData, function (err, fileObj) {
            if (err) console.log(err)
            else {
                //Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP
                console.log(fileObj);
                return fileObj._id;
            }
        });
    }
});

but it still doesn't work, please help me how to fix this. 但它仍然无法正常工作,请帮我解决这个问题。 how to insert on server side? 如何在服务器端插入?

An example for you. 一个例子给你。 I didnt tested it but it shows the way you have to go. 我没有测试它,但它显示了你必须去的方式。

First define a collection: 首先定义一个集合:

I think this step is already clear to you. 我认为这一步已经很清楚了。

var postImagesStoreFS = new FS.Store.FileSystem("postImages", {
  path: "~/workspace/uploads/"
});

Add added some filters. 添加添加一些过滤器。 Just in case you need something like that. 以防万一你需要这样的东西。

PostImages = new FS.Collection('postImages', {
  stores: [postImagesStoreFS ],
  filter: {
  maxSize: 3145728,
  allow: {
    contentTypes: ['image/*'],
    extensions: ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG']
  }
});

Now you can define in the same *.js file your allow and deny functions. 现在,您可以在相同的* .js文件中定义allow和deny函数。 If you remove the insecure package all inserts/updates/removes have to pass allow/deny functions. 如果删除不安全的包,则所有insert / updates / removed必须通过allow / deny函数。 If a command passes the allow callback it can be inserted into your collection (If there is no deny function that invalidates it) 如果一个命令通过了允许回调,它就可以插入到你的集合中(如果没有deny函数使它无效)

Well in this example i just like to insert an image if there is a user and if the metadata user of the image is the user itself. 在这个例子中,我只想插入图像,如果有用户,并且图像的元数据用户是用户自己。 You have to set the metadata user on your own. 您必须自己设置元数据用户。 For testing just return true in every allow function, like shown in the example of Pent. 对于测试,只需在每个允许函数中返回true,如Pent示例中所示。 Check the meteor documentation to read more about allow/deny http://docs.meteor.com/#allow 查看meteor文档以阅读有关允许/拒绝http://docs.meteor.com/#allow的更多信息

PostImages.allow({
  insert: function(userId, doc) {
    return (userId && doc.metadata.owner === userId);
  },
  update: function(userId, doc, fieldNames, modifier) {
    return (userId === doc.metadata.owner);
  },
  remove: function(userId, doc) {
    return false;
  },
  download: function(userId) {
    return !!userId;
  }
});

The client template should work as you posted. 客户端模板应该像您发布的那样工作。 Just in case you want to use some metadata i added a bigger example. 为了防止你想使用一些元数据我添加了一个更大的例子。

Template.myForm.events({
  'change .myFileInput': function(event, template) {
    FS.Utility.eachFile(event, function(file) {
      var fsFile = new FS.File(file);
      fsFile.metadata = {owner: Meteor.userId()};
      Images.insert(fsFile, function (err, fileObj) {

      });
    });
  }
});

This should be everything you need to have. 这应该是你需要的一切。

make sure to apply allow and deny rules like so: 确保应用允许和拒绝规则,如下所示:

Images.allow({
  insert: function() { return true },
  update: function() { return true },
  remove: function() { return false }
});

Update must also be applied if using streaming 如果使用流式传输,也必须应用更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM