简体   繁体   中英

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;
        }
    })
 });

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM