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

2.1k views Asked by At

I am trying to get the file location/url from a image I have just inserted. I have tried to use the path + objectID but the image is not present. I am trying to store the image url in another collection. So I can load that collection into my template and use the iamge url as img source.

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

There are 1 answers

8
Nate On BEST ANSWER

The path is at fileObj.url(). If you have different stores you can get the url to different stores using fileObj.url("storeName").

If you want to set an image src use a technique like so:

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

Where:

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