NicolaeBozbici
NicolaeBozbici

Reputation: 293

Can't access uploaded images - Meteor GridFS

I am trying to make an upload images feature on Meteor using GridFS and everything seems to work but I cannot access the files.

I am not sure they were successfully uploaded, but Images and EntriesImages collections are successfully populated with info.

Here's my code:

// 1. Packages
cfs:standard-packages
cfs:gridfs

// --------------------


// 2. Defining the collections + schema (collections folder)
Images = new Mongo.Collection('images');

EntriesImages = new FS.Collection("EntriesImages", {
   stores: [new FS.Store.GridFS('EntriesImages')]
});

ImagesSchema = new SimpleSchema({
    image: {
        type: String,
        label: "Image"
    },
    author: {
        type: String,
        label: "Author",
        autoValue: function(){
            return this.userId
        },
        autoform: {
            type: "hidden"
        }
    }
});
Images.attachSchema(ImagesSchema);

// --------------------

// 3. Publishing the collections (server folder)
Meteor.publish('images', function(){
    return Images.find({author: this.userId});
});
Meteor.publish('EntriesImages', function(){
    return EntriesImages.find();
});
// --------------------


// 4. Subscribe and upload functionality(client folder)
Meteor.subscribe('images');
Meteor.subscribe('EntriesImages');

Template.addImages.helpers({
    images: ()=> {
        return Images.find({});
    }
});

Template.addImages.events({
    'submit .add-images': function(event, template) {
        // get the image from the form
        var file = $('.imageInput').get(0).files[0];

        // check if img was uploaded
        if(file){
            fsFile = new FS.File(file);

            EntriesImages.insert(fsFile, function(err, fileObj){
                // if there is no error we make an insert
                if(!err){
                    var productImage = '/cfs/files/ProductsImages/' + fileObj._id;
                    Images.insert({
                        image: productImage
                    });
                }
            });

        } else {
            console.log('something bad happend');
        }

        return false;
    }
});

Using this code, my Images collection gets populated with and entry as I would expect:

// sample entry
{
    "_id": "Qp8Hsgki5G9DT2dGz",
    "image": "/cfs/files/ProductsImages/drbbaj7BKzttzzgZX",
    "author": "TMviRL8otm3ZsddSt"
}

and the cfs.EntriesImages.filerecord also gets populated with one entry:

{
  "_id": "x4Sei5sbnFwDii4HE",
  "original": {
    "name": "dash.png",
    "updatedAt": "2015-12-15T14:32:10.000Z",
    "size": 542121,
    "type": "image/png"
  },
  "uploadedAt": "2016-01-18T13:08:34.914Z",
  "copies": {
    "EntriesImages": {
      "name": "dash.png",
      "type": "image/png",
      "size": 542121,
      "key": "569ce3d2e28bd3035ba03735",
      "updatedAt": "2016-01-18T13:08:34.937Z",
      "createdAt": "2016-01-18T13:08:34.937Z"
    }
  }
}

Problem is I still cannot access the image(if it exists at all) using the url that gets saved in the db.

I also tried with insecure and autopublish enabled but it's still not working..

Upvotes: 4

Views: 759

Answers (2)

Martins Untals
Martins Untals

Reputation: 2288

It is important to allow 'download' action on the gridfs collection in server side code:

EntriesImages.allow({    
  download: function(userId, fileObj) {
     return true
 }
});

without this, url will not work

Upvotes: 2

NicolaeBozbici
NicolaeBozbici

Reputation: 293

While I could not get the code above to run as expected, I managed to get the image upload running by following this guide: How to upload files with Meteor JS

Upvotes: 0

Related Questions