如何从CollectionFS(流星)获取图像的位置/ URL

汤姆·沃辛

我正在尝试从刚插入的图像获取文件位置/ URL。我试图使用路径+ objectID,但是图像不存在。我正在尝试将图像网址存储在另一个集合中。因此,我可以将该集合加载到模板中,并使用iamge网址作为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' }});

  }
});
内特

路径在fileObj.url()如果您有其他商店,则可以使用来获取指向其他商店的网址fileObj.url("storeName")

如果要设置图像src,请使用如下技术:

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

在哪里:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章