我试图了解如何拍摄图片(使用CollectionFS的文件)并将图片的ID插入我的商品imageId
字段:
LIB /集合/ items.js
Items = new Mongo.Collection("items");
Items.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Name",
},
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoform: {
type: "hidden",
label: false
},
autoValue: function () { return Meteor.userId() },
},
image: {
type: String,
optional: true,
autoform: {
label: false,
afFieldInput: {
type: "fileUpload",
collection: "Images",
label: 'Select Photo',
}
}
},
imageId: {
type: String
}
}));
LIB /集合/ images.js
if (Meteor.isServer) {
var imageStore = new FS.Store.S3("images", {
accessKeyId: Meteor.settings.AWSAccessKeyId,
secretAccessKey: Meteor.settings.AWSSecretAccessKey,
bucket: Meteor.settings.AWSBucket,
});
Images = new FS.Collection("Images", {
stores: [imageStore],
filter: {
allow: {
contentTypes: ['image/*']
}
}
});
}
// On the client just create a generic FS Store as don't have
// access (or want access) to S3 settings on client
if (Meteor.isClient) {
var imageStore = new FS.Store.S3("images");
Images = new FS.Collection("Images", {
stores: [imageStore],
filter: {
allow: {
contentTypes: ['image/*']
},
}
});
}
现在我的允许规则是:
服务器/ allows.js
Items.allow({
insert: function(userId, doc){return doc && doc.userId === userId;},
update: function(userId, doc){ return doc && doc.userId === userId;},
remove: function(userId, doc) { return doc && doc.userId === userId;},
})
Images.allow({
insert: function(userId, doc) { return true; },
update: function(userId,doc) { return true; },
remove: function(userId,doc) { return true; },
download: function(userId, doc) {return true;},
});
我使用Autoform,所以我的表单看起来像这样:
的客户机/ item_form.html
<template name="insertItemForm">
{{#autoForm collection="Items" id="insertItemForm" type="insert"}}
{{> afQuickField name="name" autocomplete="off"}}
{{> afQuickField name="image" id="imageFile"}}
<button type="submit">Continue</button>
{{/autoForm}}
</template>
现在,当我选择浏览并选择一个图像时,它将出现在数据库中,我想把它带有_id
并将其放在之后创建的Item
中,但是如何做我拿那张特别的照片?我认为这是引用图像的好方法。
更新1
找出在选择文件后隐藏ID的实际位置:
<input type="hidden" class="js-value" data-schema-key="image" value="ma633fFpKHYewCRm8">
所以我试图将ma633fFpKHYewCRm8
作为String
放在ImageId
中。
更新2
也许一种方法是使用FS.File Reference?
答案 0 :(得分:1)
我已经解决了相同的问题而且更简单,插入文件后,我只是调用一个执行相关集合更新的方法:
client.html
UIImageView
lib.js
<template name="hello">
<p>upload file for first texture: <input id="myFileInput1" type="file"> </p>
</template>
client.js
var textureStore = new FS.Store.GridFS("textures");
TextureFiles = new FS.Collection("textures", {
stores: [textureStore]
});
Textures = new Mongo.Collection("textures");
server.js
Template.hello.events({
'change #myFileInput1': function(event, template) {
uploadTextureToDb('first',event);
}
});
function uploadTextureToDb(name, event) {
FS.Utility.eachFile(event, function(file) {
TextureFiles.insert(file, function (err, fileObj) {
// Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP
console.log('inserted');
console.log(fileObj);
//after file itself is inserted, we also update Texture object with reference to this file
Meteor.call('updateTexture',name,fileObj._id);
});
});
}
当你使用autoForm和simpleSchema时,它可能不那么容易,但我建议你先忘掉autoForm和simpleSchema,然后尝试使用简单的html和默认集合。
一切正常后,你可以回去设置它们,但要注意CollectionFS可能会有更多问题,特别是当涉及到autoForm生成的样式时。