[英]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.