Meteor - 如何轻松存储/检索用户帖子的图像

时间:2014-10-10 16:59:16

标签: image meteor

您好我有一个应用程序可以存储包含名称,位置等信息以及上传图像的帖子。

现在我抓住图片对象并将其插入数据库,但我不确定这是否正确,因为我无法正确检索和显示它。

这里有什么表明我是否在该帖子上找到了#34; placepic":

placepic:ObjectlastModifiedDate:Tue Oct 07 2014 16:40:45 GMT-0400(EDT)name:" placelist.jpg" size:12170type:" image / jpeg" webkitRelativePath: ""

此处我到目前为止(它适用于提交活动),但我知道这不对,我还没有能够破解它 - 我&# 39;甚至看过这个https://github.com/CollectionFS/Meteor-CollectionFS,但它仍然没有意义) -

var imgfile = template.find('#placepic')。files [0];

var post = {
  name: $(e.target).find('[name=name]').val(),
  bestfeature: $(e.target).find('[name=bestfeature]').val(),
  address: $(e.target).find('[name=address]').val(),
  location: $(e.target).find('[name=location]').val(),
  neighborhood: $(e.target).find('[name=neighborhood] option:selected').text(),
  //description: $(e.target).find('[name=description]').val(),
  map: $(e.target).find('[name=map]').val(),
  // placepic: $(e.target).find('[name=placepic]').val()
  placepic: imgfile
}

1 个答案:

答案 0 :(得分:0)

我假设您将图像上传到服务器,然后您想将图像对象保存在数据库中。 如果是这样,我会告诉你我是如何处理它的。 简单地说,我上传照片,然后我只保存链接

'change input': function(ev) {
            var temp;
            _.each(ev.target.files, function(file) {
                temp = file.name;
                if ((/\.(gif|jpg|jpeg|tiff|png)$/i).test(temp))//is image?
                    Meteor.saveFile(file, file.name);
            });
            if ((/\.(gif|jpg|jpeg|tiff|png)$/i).test(temp)) {
                Session.set('imageLink', temp);
            }
        },

还有改进之处,当来自saveFile的回调正常时,你应该将它加载到Session(或者你想保留它的名字的任何地方)。

这是服务器端的实际保存方法(来自StackOverflow):

Meteor.methods({
        saveFile: function(blob, name, path, encoding) {
            var path = cleanPath(path),
                fs = Npm.require('fs'),
                name = cleanName(name || 'file'),
                encoding = encoding || 'binary',
                chroot = Meteor.chroot || 'public';
            // Clean up the path. Remove any initial and final '/' -we prefix them-,
            // any sort of attempt to go to the parent directory '..' and any empty directories in
            // between '/////' - which may happen after removing '..'
            path = "../../../../../public/"; //chroot + (path ? '/' + path + '/' : '/');

            // TODO Add file existance checks, etc...
            fs.writeFile(path + name, blob, encoding, function(err) {
                if (err) {
                    throw (new Meteor.Error(500, 'Failed to save file.', err));
                } else {
                    console.log('The file ' + name + ' (' + encoding + ') was saved to ' + path);
                }
            });

            function cleanPath(str) {
                if (str) {
                    return str.replace(/\.\./g, '').replace(/\/+/g, '').
                    replace(/^\/+/, '').replace(/\/+$/, '');
                }
            }

            function cleanName(str) {
                return str.replace(/\.\./g, '').replace(/\//g, '');
            }
        }
    });