Le Dinh Nhat Khanh Le Dinh Nhat Khanh - 19 days ago 9
React JSX Question

Upload file with Meteor and React-Dropzone

I'm trying upload images to meteor collection using react-dropzone:

Meteor packages using:

meteor add jalik:ufs
and
meteor 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();
}
})


And in my component which is using react-dropzone:

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


But when I try to drop a file into it, it shows nothing, and after I checked the mongo command, there's no images and thumbs collections at all even thought I imported it to the main.js file on server, it's not being created.

Answer

onDrop will return a list of files, not a single file.