How to upload files with Meteor.js?

Victor Leung
Dec 22, 2014 · 2 min read
meteor add cfs:standard-packages
meteor add cfs:gridfs
var imageStore = new FS.Store.GridFS(“images”);

Images = new FS.Collection(“images”, {
stores: [imageStore]
});
Images.deny({
insert: function(){
return false;
},
update: function(){
return false;
},
remove: function(){
return false;
},
download: function(){
return false;
}
});

Images.allow({
insert: function(){
return true;
},
update: function(){
return true;
},
remove: function(){
return true;
},
download: function(){
return true;
}
});
<input type=”file” name=”…” class=”myFileInput”>
Template.Profile.events({
‘change .myFileInput’: function(event, template) {
FS.Utility.eachFile(event, function(file) {
Images.insert(file, function (err, fileObj) {
if (err){
// handle error
} else {
// handle success depending what you need to do
var userId = Meteor.userId();
var imagesURL = {
“profile.image”: “/cfs/files/images/“ + fileObj._id
};
Meteor.users.update(userId, {$set: imagesURL});
}
});
});
},
Meteor.publish(“images”, function(){ return Images.find(); });
Router.route(‘/profile’,{
waitOn: function () {
return Meteor.subscribe(‘images’)
},
action: function () {
if (this.ready())
this.render(‘Profile’);
else
this.render(‘Loading’);
}
});

    Victor Leung

    Written by

    I’m a keen traveler to see every country in the world, passionate about cutting edge technologies.

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade