您可以使用很多软件包。
我推荐CollectionFS。
您需要添加这 3 个包,一切就绪。
cfs:标准包
cfs:gridfs // 存储适配器包。如果你愿意,你可以改变它。
cfs:文件系统
让我们从插入图片开始。
1.在你的 lib 文件夹中创建 ImageCollection.js
import { Mongo } from 'meteor/mongo';
export const BOOK = new Mongo.Collection('books');
var imageStore = new FS.Store.GridFS("images");
export const Images = new FS.Collection("images", {
stores: [imageStore]
});Images.deny({
insert: function(){
return false;
},
update: function(){
return false;
},
remove: function(){
return false;
},
download: function(){
return false;
}
});
Images.allow({
insert: function(){
return true;
},
update: function(){
return true;
},
remove: function(){
return true;
},
download: function(){
return true;
}
})
2。在客户端和服务器端导入图像集合。
例如,
import {Images} from '../lib/imageCollection';
3.在表单中添加输入类型“文件”并根据您的使用情况。
4.在该模板的 .JS 文件中创建更改事件。
'change #bookCover': function (event) {
event.preventDefault();
console.log("changed!")
var files = event.target.files;
for (var i = 0, ln = files.length; i < ln; i++) {
Images.insert(files[i], function (err, fileObj) {
// Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP
bookImgId=fileObj._id;
});
}
},
将插入您的数据库图像。
5.显示图像 将此 HTML 添加到您要查看图像的位置。
6.将此代码添加到您要显示图像的 js 文件中。
bookImage: function (id) {
// console.log(id);
var imageBook = Images.findOne({_id:id});
// console.log("img: "+imageBook);
var imageUrl = imageBook.url();
return imageUrl; // Where Images is an FS.Collection instance
}
注意:确保将图书收藏导入到要显示图片的位置。