首页 文章

Meteor文件在Mongo集合中存储图像URL

提问于
浏览
0

我'm really lost when it comes to file uploading in meteor and manage the data between client and server. I'm使用Veliov Group的Meteor Files在客户端上传多个图像 . 它们被存储在 FilesCollection 被称为图像中,而我的 Mongo.Collection 被称为广告 .

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

我'm trying to achieve is, when I upload the images, I wanna store the URLs on the document in Adverts that I'与之合作(我正在使用 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}})
  },

这可能是一个愚蠢的问题,一切都可能在文档中,但我来回抄袭这些文档,我无法理解我需要做什么 .

1 回答

  • 0

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

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

相关问题