我正在尝试从我刚刚插入的图像中获取文件位置/url。我尝试使用路径 + objectID,但图像不存在。我正在尝试将图像 url 存储在另一个集合中。所以我可以将该集合加载到我的模板中,并使用 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);
服务器.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});
}
}
主页.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>
主页.js
Template.home.helpers({
shopList: function() {
var search_query = Session.get('search_query');
return Shops.find({name: { $regex: search_query + ".*", $options: 'i' }});
}
});