如何从CollectionFS(Meteor)

时间:2015-06-07 22:04:10

标签: node.js image mongodb meteor collectionfs

我正在尝试从刚插入的图像中获取文件位置/网址。我试图使用路径+ objectID但图像不存在。我试图将图像网址存储在另一个集合中。所以我可以将该集合加载到我的模板中并使用iamge url作为img源。

var ShopsImageFS = new FS.Store.FileSystem("images", {
  path: "~/shop/images/"
});

ShopsImages = new FS.Collection("images", {
  stores: [ShopsImageFS],
  filter: {
    maxSize: 3145728,
    allow: {
  contentTypes: ['image/*'],
  extensions: ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG']
}}
});

Template.newshop.events({
  "change .image": function(event, template){
    FS.Utility.eachFile(event, function(file) {
      ShopsImages.insert(file, function (err, fileObj){
        if(err) {
          console.log(err);
        } else {

          Session.set("imageURL", "~/shop/images/" + fileObj);
          // not working
        }
      })
    })
  },

var imageURL = Session.get('imageURL');

Meteor.call("addShop", date, name, description, imageURL, address, postcode, city, country, latitude, longitude);

server.js

Meteor.startup(function(){
    Meteor.methods({
      addShop:function (date, name, description, imageURL ,address, postcode, city, country, latitude, longitude) {

          Shops.insert({date:date, name:name, description:description, imageURL:imageURL, address:address, postcode:postcode, city:city, country:country,  latitude:latitude, longitude:longitude});
    }
}

home.html的

<div class="col-md-6">
      {{#each shopList}}
        {{>shopCard}}
      {{/each}}
    </div>

<template name="shopCard">

<a style="text-decoration: none;" href="{{ pathFor route='shop.show' name=name _id=_id }}">
  <div id="shopCardItem">

  <div class="panel panel-default">
  <div class="panel-body">

    <img id="shopImage" src="{{imageURL}}" alt="{{name}}"/>

    <div id="shopContent">
      <h1>{{name}}</h1>
      <p>{{city}}</p>

    </div>

  </div>

</div>

</div>

</a>

</template>

home.js

Template.home.helpers({
  shopList: function() {
    var search_query = Session.get('search_query');
    return Shops.find({name: { $regex: search_query + ".*", $options: 'i' }});

  }
});

1 个答案:

答案 0 :(得分:1)

路径位于fileObj.url()。如果您有不同的商店,则可以使用fileObj.url("storeName")获取到不同商店的网址。

如果你想设置一个图像src使用这样的技术:

{{#each shopimage}}
    <img src="{{this.url store='images'}}">
{{/each}}

其中:

 shopimage : function () {
     return ShopImages.find();
 }