2014-10-10 20 views
0

嗨我有一个应用程序,存储职位,其中包括名称,位置等信息,也上传的图像。流星 - 我如何轻松地存储/检索用户帖子的图像

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

这里,如果我做一个找到该职位 “placepic” 显示的内容:

placepic:ObjectlastModifiedDate:周二2014年10月7日16点四十〇分45秒格林尼治标准时间0400(EDT)名称: “placelist.jpg”大小:12170type:“image/jpeg”webkitRelativePath:“”

这是我到目前为止(它在一个提交事件),但我知道这是不对的,我一直无法破解它 - 我甚至看过这个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 
} 

回答

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的回调来确定,那么你应该将其加载到会议(或任何你想保留名称)。

这里是(从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, ''); 
      } 
     } 
    }); 
+0

酷。是的,我终于得到(一半)https://github.com/CollectionFS/Meteor-CollectionFS工作。它存储图像(尽管到.meteor/local/cfs/files/images中的路径不太好),试图找出如何获取服务器上文件的url路径并将其保存到一个变量。我会尝试以上。谢谢 – cmee 2014-10-11 00:36:35

+0

AHA!我想到了。你的代码激励我去破解路径,以及如何为保存路径和流星路径设置目录文件保存位置的路径。谢谢 - 你救了我几个小时把我的头撞在墙上。 – cmee 2014-10-11 18:19:42

+0

此问题已解决。 – cmee 2014-10-11 18:20:18