流星:如何在mongodb集合中存储和检索文件?



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

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

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

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

最新更新