使用Meteor和React-Dropzone上传文件

时间:2016-11-15 04:42:59

标签: mongodb reactjs meteor image-uploading

我正在使用react-dropzone尝试将图像上传到流星集合:

Meteor包使用:meteor add jalik:ufsmeteor add jalik:ufs-gridfs

imports/api/images

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { UploadFS } from 'meteor/jalik:ufs';

export const Images = new Mongo.Collection('images');
export const Thumbs = new Mongo.Collection('thumbs');

if(Meteor.isServer) {
    Meteor.publish('thumbs', function(id) {
        return Thumbs.find({
            originalStore: 'images',
            originalId: {
                $in: ids
            }
        });
    })

    Meteor.publish('images', function() {
        return Images.find({});
    })
}

function loggedIn(userId) {
  return !!userId;
}

export const ThumbsStore = new UploadFS.store.GridFS({
  collection: Thumbs,
  name: 'thumbs',
  permissions: new UploadFS.StorePermissions({
    insert: loggedIn,
    update: loggedIn,
    remove: loggedIn
  }),
  transformWrite(from, to, fileId, file) {
    // Resize to 32x32
    const gm = require('gm');

    gm(from, file.name)
      .resize(32, 32)
      .gravity('Center')
      .extent(32, 32)
      .quality(75)
      .stream()
      .pipe(to);
  }
});

export const ImagesStore = new UploadFS.store.GridFS({
    collection: Images,
    name: 'images',
    permissions: new UploadFS.StorePermissions({
        insert: loggedIn,
        update: loggedIn,
        remove: loggedIn
    }),
    filter: new UploadFS.Filter({
        contentTypes: ['image/*']
    }),
    copyTo: [
        ThumbsStore
    ]
});

Meteor.methods({
    upload: function(file) {
        const photo = {
            name: file.name,
            type: file.type,
            size: file.size
        };

        const upload = new UploadFS.Uploader({
            data: file,
            file: photo,
            store: ImagesStore,
            onError: () => {
                console.log('error');
            },
            onComplete: console.log('Completed'),
        });

        upload.start();
    }
})

在我的组件中使用react-dropzone:

onDrop(file) {
        UploadFS.selectFile(() => {
            Meteor.call('upload', file)
        })
    }

但是当我尝试将文件放入其中时,它什么也没有显示,在我检查了mongo命令之后,根本没有图像和拇指集合,甚至认为我将它导入到main.js文件中服务器,它没有被创建。

1 个答案:

答案 0 :(得分:1)

onDrop将返回文件列表,而不是单个文件。