Meteor collectionfs 插入服务器端 [英] Meteor collectionfs insert server side

查看:15
本文介绍了Meteor collectionfs 插入服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我使用 collectionfs + gridfs + cfs 文件系统,在 collectionfs 文档中,我找到了如何在客户端插入文件,如下所示:

hi everyone i using collectionfs + gridfs + cfs filesystem, on collectionfs documentation i find how to insert file on client side like this :

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.

首先定义一个集合:

我想这一步你已经很清楚了.

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']
  }
});

现在您可以在同一个 *.js 文件中定义允许和拒绝函数.如果您删除不安全的包,则所有插入/更新/删除都必须通过允许/拒绝功能.如果命令通过了允许回调,则可以将其插入到您的集合中(如果没有使它无效的拒绝函数)

Now you can define in the same *.js file your allow and deny functions. If you remove the insecure package all inserts/updates/removes have to pass allow/deny functions. If a command passes the allow callback it can be inserted into your collection (If there is no deny function that invalidates it)

在这个例子中,如果有用户并且图像的元数据用户是用户本身,我只想插入图像.您必须自己设置元数据用户.对于测试,只需在每个允许函数中返回 true,如 Pent 示例所示.查看meteor 文档以了解有关允许/拒绝的更多信息 http://docs.meteor.com/#allow

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. Check the meteor documentation to read more about allow/deny 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.

这篇关于Meteor collectionfs 插入服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆