【问题标题】:Meteor Files Storing a image url in Mongo collection流星文件在 Mongo 集合中存储图像 url
【发布时间】:2018-01-31 17:19:26
【问题描述】:

当谈到在流星中上传文件和管理客户端和服务器之间的数据时,我真的很迷茫。 我正在使用 Veliov Group 的 Meteor Files 在客户端上传多个图像。它们被存储在名为 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}})
  },

这可能是一个愚蠢的问题,文档中可能包含所有内容,但我反复阅读了这些文档,但无法理解我需要做什么。

【问题讨论】:

  • 第一:你的服务器方法不是正确的mongo更新,请阅读docs.mongodb.com/manual/reference/operator/update/set 第二:你可以在文件上传时直接在服务器上插入新的url,阅读onAfterUpload在ostrio:files wiki
  • 我可以使用 onAfterUpload 访问文件数据,但我只能获取路径,而不是 url。即使我得到了 url,我如何将它存储在客户端正在使用的文档中?我很困惑

标签: javascript mongodb meteor


【解决方案1】:

我的问题的答案是做服务器端

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 }}})

【讨论】:

    猜你喜欢
    • 2015-04-01
    • 2016-01-11
    • 2018-05-03
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    相关资源
    最近更新 更多