[英]Meteor Files Storing a image url in Mongo collection
当我在流星中上传文件并管理客户端和服务器之间的数据时,我真的迷失了。 我正在使用Veliov Group的Meteor文件在客户端上载多个图像。 它们被存储在名为Images的FilesCollection
,而我的Mongo.Collection
称为Adverts。
collections.js:
Adverts = new Mongo.Collection('adverts');
Images = new FilesCollection({
collectionName: 'Images',
storagePath: () => {
return `~/public/uploads/`;
},
allowClientCode: true, // Required to let you remove uploaded file
onBeforeUpload(file) {
// Allow upload files under 10MB, and only in png/jpg/jpeg formats
if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.ext)) {
return true;
} else {
return 'Limit 10mb';
}
}
});
// if client subscribe images
if (Meteor.isClient) {
Meteor.subscribe('files.images.all');
};
// if server publish images
if (Meteor.isServer) {
Images.allowClient();
Meteor.publish('files.images.all', () => {
return Images.collection.find();
});
};
我想要实现的是,当我上传图像时,我想将URL存储在正在使用的广告中的文档上(我正在使用iron:router
访问这些文档_id)。 我设法获取了URL,但仅针对上传的第一张图片,获取了我在文档中看到的代码:
Template.imageUpload.helpers({
imageFile: function () {
return Images.collection.findOne();
},
myImage: () => {
console.log(Images.findOne({}).link())
}
})
Template.imageUpload.events({
'change #fileInput': function (e, template) {
if (e.currentTarget.files) {
_.each(e.currentTarget.files, function (file) {
Images.insert({
file: file
});
});
}
}
})
我正在使用Meteor.Call将URL发送到服务器,但是我无法使用新的属性pic
和图像的值url
更新文档
server.js:
imageUpload: (actDoc, imgURL) => { // actDoc is the document id that I'm working on the client
Adverts.update({'reference': actDoc}, {$set: {'pic': imgURL}})
},
这可能是一个愚蠢的问题,文档中的所有内容都可能会出现,但是我已经来回阅读了这些文档,我无法理解自己需要做什么。
我的问题的答案是在服务器端进行操作
main.js server
FSCollection.on('afterUpload'), function (fileRef) {
var url = 'http://localhost:3000/cdn/storage/images/' + fileRef._id + '/original/' + fileRef._id + fileRef.extensionWithDot;
}
MongoCollection.update({'_id': docId}, { $set: {url: imgUrl }}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.