我发现你的问题非常有趣,所以我我开始想办法了。我对Meteor和Node.js非常陌生,所以我认为有更好理解的人可以想出更好的解决方案,但我的基本想法是将每个映像从目录导入到CollectionFS。
这里是我想出了(当心,这是一个10分钟的实物模型而不是复制粘贴的解决方案!)
var basedir = '../../../../../public/';
var imageStore = new FS.Store.FileSystem("images", {
path: basedir
});
Images = new FS.Collection("images", {
stores: [imageStore]
});
if (Meteor.isServer) {
Images.allow({
'insert': function() {
return true;
},
'download' : function(){
return true;
}
});
function importFiles(importDir) {
var dir = basedir + importDir;
var files = fs.readdirSync(dir);
_(files).each(function(f){
fs.readFile(dir + f, Meteor.bindEnvironment(function (err, data) {
if (err) throw err;
var newFile = new FS.File();
newFile.attachData(data, {type: 'image/png'});
newFile.name(f);
var insertedFile = Images.insert(newFile,function(err,fob){
if (err) throw err;
console.log('Uploaded successfully');
});
}));
})
}
importFiles('import/'); // this dir in under my public folder
}
此代码导入一切从公共/导入到目录规定为imageStore
。 不过要小心,因为它读取整个文件,fs.readFile
您需要安装Node.js的FS库这可能会导致严重的内存问题meteor add peerlibrary:fs
不要忘了过滤文件列表和设置例如,基于扩展名修正MIME类型。
导入图片后,您可以使用CollectionFS find()`` to list the images and then
fileObject.url()```来显示它们。
有CollectionFS GitHub的页面例如为: https://github.com/CollectionFS/Meteor-CollectionFS/#url
的例子