javascript - Meteor collectionfs insert server side - Stack Overflow

hi everyone i using collectionfs + gridfs + cfs filesystem,on collectionfs documentation i find how to

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 :

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?

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 :

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?

Share Improve this question asked Apr 13, 2014 at 14:57 yozawiratamayozawiratama 4,32812 gold badges65 silver badges108 bronze badges 4
  • It is not working on client side because you have to define allow/deny rules. I remend you to do it on clientside ;) Check the meteor docs. – chaosbohne Commented Apr 13, 2014 at 15:07
  • :o but i remove insecure, so cant insert from client, it always error – yozawiratama Commented Apr 13, 2014 at 16:14
  • Did you defined allow/deny rules? – chaosbohne Commented Apr 13, 2014 at 16:29
  • Oops sorry i dont know about that, where i must define it? May be there is an example? – yozawiratama Commented Apr 13, 2014 at 16:48
Add a ment  | 

2 Answers 2

Reset to default 7

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. If you remove the insecure package all inserts/updates/removes have to pass allow/deny functions. If a mand passes the allow callback it can be inserted into your collection (If there is no deny function that invalidates it)

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./#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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742335957a4424653.html

相关推荐

  • javascript - Meteor collectionfs insert server side - Stack Overflow

    hi everyone i using collectionfs + gridfs + cfs filesystem,on collectionfs documentation i find how to

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信