【问题标题】:Meteor: How to store and retrive file in a mongodb collection?Meteor:如何在 mongodb 集合中存储和检索文件?
【发布时间】:2019-10-16 14:31:15
【问题描述】:

我尝试将上传文件的内容保存在一个集合中:

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

在另一个文件中,我想检索保存的文件。首先,我不知道我应该通过什么 id 。假设集合中有一个文件,或者我想要第一个文件,或者当前用户拥有的文件。我试过了,我将 fileId 作为 1 传递,但它没有用。我实际上不知道下面的问号:

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

【问题讨论】:

  • 我的回答对您有帮助吗?还是有问题?
  • @Jankapunkt 感谢您的回答,但是,这是一个老问题,目前我离开了 Meteor。

标签: javascript mongodb meteor


【解决方案1】:

有一个特殊的层用于将文件存储为块,称为GridFs,因为当您尝试存储大于 16MB 的二进制文件时,正常的集合会出现问题。

Meteor 中还有一个完善的包装器用于处理文件。查看以下包:https://github.com/VeliovGroup/Meteor-Files

【讨论】:

    猜你喜欢
    • 2013-10-15
    • 2015-11-08
    • 2017-03-28
    • 2012-05-26
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多