流星文件在 Mongo 集合中存储图像 url [英] Meteor Files Storing a image url in Mongo collection
问题描述
当涉及到在meteor 中上传文件和管理客户端和服务器之间的数据时,我真的很迷茫.我正在使用 Veliov Group 的 Meteor Files 在客户端上传多个图像.它们被存储在名为 Images 的 FilesCollection
中,而我的 Mongo.Collection
名为 Adverts.
I'm really lost when it comes to file uploading in meteor and manage the data between client and server.
I'm using Meteor Files from Veliov Group to upload multiple images on the client side. They're getting stored in a FilesCollection
called Images and I have my Mongo.Collection
called Adverts.
collections.js:
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,但仅限于上传的第一张图片,我在文档中看到的代码:
What I'm trying to achieve is, when I upload the images, I wanna store the URLs on the document in Adverts that I'm working with (I'm using iron:router
to access those documents _id).
I managed to get the URL but only for the first image uploaded, my code for what I saw on the docs:
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
更新文档> 图像
I was using a Meteor.Call to send the URL to the server, but I couldn't manage to update the document with a new property pic
and the value url
of the image
server.js:
imageUpload: (actDoc, imgURL) => { // actDoc is the document id that I'm working on the client
Adverts.update({'reference': actDoc}, {$set: {'pic': imgURL}})
},
这可能是一个愚蠢的问题,所有内容都可能在文档中,但我反复阅读了这些文档,但我无法理解我需要做什么.
This is probably a dumb question and everything might in the docs, but I've readed those docs back and forth and I can't manage to understand what I need to do.
推荐答案
我的问题的答案是在服务器端做
The answer for my problem was to do it server side
main.js 服务器
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 }}})
这篇关于流星文件在 Mongo 集合中存储图像 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!