简体   繁体   English

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

[英]Meteor - How do I easily store / retrieve images for user posts

Hi I have an application that stores posts that include information such as name, location etc and also an uploaded image. 嗨,我有一个应用程序,用于存储包含名称,位置等信息以及上载图像的帖子。

Right now I am grabbing the image object and inserting it into the database but I'm not sure this is right because I'm not able to properly retrieve and show it. 现在,我正在抓取图像对象并将其插入数据库,但是我不确定这是正确的,因为我无法正确检索和显示它。

Here's what shows if I do a find on that post for "placepic": 这是显示我是否在该帖子中找到“ placepic”的内容:

placepic: ObjectlastModifiedDate: Tue Oct 07 2014 16:40:45 GMT-0400 (EDT)name: "placelist.jpg"size: 12170type: "image/jpeg"webkitRelativePath: "" 地点图片:ObjectlastModifiedDate:2014年10月7日星期二GMT-0400(EDT)名称:“ placelist.jpg”大小:12170类型:“ image / jpeg” webkitRelativePath:“”

Here's where I'm at so far (it works on a submit event) but I know this isn't right and I haven't been able to crack it - I've even looked at this https://github.com/CollectionFS/Meteor-CollectionFS but it still doesn't make sense) - 到目前为止,这里是我的位置(它在提交事件上有效),但是我知道这是不对的,而且我无法破解它-我什至查看了这个https://github.com/ CollectionFS / Meteor-CollectionFS,但仍然没有意义)-

var imgfile = template.find('#placepic').files[0]; 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
}

I assume you upload your image to the server, and then you want to save image object in the database. 我假设您将图像上载到服务器,然后要将图像对象保存在数据库中。 If so, I'll show you how I handled it. 如果是这样,我将告诉您如何处理。 Simply, I upload photo, and then I just save link to it 只需上传照片,然后保存链接即可

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

There is place for improvement, when callback from saveFile comes OK, then you should load it to Session(or wherever you want to keep name of it). 有需要改进的地方,当从saveFile进行回调可以,然后应将其加载到Session(或任何要保留其名称的位置)。

And here is the actual save method on server side(from StackOverflow): 这是服务器端的实际保存方法(来自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, '');
            }
        }
    });

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

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