簡體   English   中英

如何從CollectionFS(流星)獲取圖像的位置/ URL

[英]How to get location/url of image from CollectionFS (Meteor)

我正在嘗試從剛插入的圖像獲取文件位置/ 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")將該URL轉到其他商店。

如果要設置圖像src,請使用如下技術:

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

哪里:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM