2015-09-06 69 views
0

你将如何访问一个文件夹的图像,获取每个图像的URL并将其放入数组,然后{{each}}在该数组上显示每页图像选择?大家一直说CollectionFS,但由于某种原因,当我设置它:流星 - 访问和显示图像

var imageStore = new FS.Store.FileSystem('images', { 
    path: '~/category/engagements' 
}); 

Images = new FS.Collection('images', { 
    stores: [imageStore] 
}); 

我可以访问控制台中的图像,但数组是空的。这不是我需要做的吗?

回答

0

CollectionFS保留了一个mongo集合,该集合实质上指向已存储在文件系统中某个位置的图像,无论是在服务器的磁盘上还是在云中。 afaik,你不能简单地将CollectionFS放在一个包含文件的目录中,你需要首先使用CollectionFS将文件放在那里(在你的情况下)Images.insert()

0

我发现你的问题非常有趣,所以我我开始想办法了。我对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

的例子