Ahmad
Ahmad

Reputation: 9658

Meteor: How to store and retrive file in a mongodb collection?

I try to save the content of uploaded file in a collection:

export const Files = new Mongo.Collection('files');

Meteor.methods({
    'saveFileToDB': function (buffer) {
        Files.insert({ data:buffer,
            createdAt: new Date(),
            owner: this.userId,
            username: Meteor.users.findOne(this.userId).username
        });
    },
});

In another file I want to retrieve the saved file. First, I don't know what id I should to pass it. Suppose that there is one file in the collection or I want the first one, or the ones owned by the current user. I tried, I passed fileId as 1 but it didn't work. I don't know actually the question marks below:

import {Files} from "./files";

Meteor.methods({
    'slides.readFileFromDB'(fileId) {
        if (Files.???) { //contain any data
           const text = Files.findOne(???);
           console.log(text);
           // Meteor.call('slides.insert', text);
           return text;
        }
    })
 });

Upvotes: -1

Views: 254

Answers (1)

Jankapunkt
Jankapunkt

Reputation: 8413

There is a special layer for storing files as chunks, called GridFs because a normal collection will get into problems when you try to store binaries larger than 16MB.

There is also a well established wrapper for handling files in Meteor. Check out the following package: https://github.com/VeliovGroup/Meteor-Files

Upvotes: 1

Related Questions