我将Meteor.js与Amazon S3 Bucket一起用于上传和存储照片。我正在使用陨石包装collectionFS和aws-s3。我已经正确设置了aws-s3连接,并且图像收集工作正常。
客户端事件处理程序:
'click .submit': function(evt, templ) {
var user = Meteor.user();
var photoFile = $('#photoInput').get(0).files[0];
if(photoFile){
var readPhoto = new FileReader();
readPhoto.onload = function(event) {
photodata = event.target.result;
console.log("calling method");
Meteor.call('uploadPhoto', photodata, user);
};
}
而我的服务器端方法:
'uploadPhoto': function uploadPhoto(photodata, user) {
var tag = Random.id([10] + "jpg");
var photoObj = new FS.File({name: tag});
photoObj.attachData(photodata);
console.log("s3 method called");
Images.insert(photoObj, function (err, fileObj) {
if(err){
console.log(err, err.stack)
}else{
console.log(fileObj._id);
}
});
选定的文件是.jpg图像文件,但是在上传时,我在服务器方法中收到此错误:
调用方法“ uploadPhoto”时发生异常错误:DataMan构造函数收到了不支持的数据
而且无论我是直接传递图像文件,还是将其作为数据附加或使用fileReader读取为文本/二进制/字符串。我仍然收到该错误。请指教。
好吧,也许有些想法。几个月前,我已经使用collectionFS做过事情,所以请多加注意文档,因为我的示例可能不是100%正确的。
凭据应通过环境变量设置。因此,您的密钥和机密仅在服务器上可用。检查此链接以进一步阅读。
好的,首先,这是一些对我有用的示例代码。检查您的差异。
模板助手:
'dropped #dropzone': function(event, template) {
addImage(event);
}
功能addImage:
function addImagePreview(event) {
//Go throw each file,
FS.Utility.eachFile(event, function(file) {
//Some Validationchecks
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
var fsFile = new FS.File(image.src);
//setMetadata, that is validated in collection
//just own user can update/remove fsFile
fsFile.metadata = {owner: Meteor.userId()};
PostImages.insert(fsFile, function (err, fileObj) {
if(err) {
console.log(err);
}
});
};
})(file);
// Read in the image file as a data URL.
reader.readAsDataURL(file);
});
}
好的,您接下来要进行验证。可以使用允许/拒绝规则以及FS.Collection上的过滤器来完成验证。这样,您可以进行所有验证并通过客户端插入。
例:
PostImages = new FS.Collection('profileImages', {
stores: [profileImagesStore],
filter: {
maxSize: 3145728,
allow: {
contentTypes: ['image/*'],
extensions: ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG']
}
},
onInvalid: function(message) {
console.log(message);
}
});
PostImages.allow({
insert: function(userId, doc) {
return (userId && doc.metadata.owner === userId);
},
update: function(userId, doc, fieldNames, modifier) {
return (userId === doc.metadata.owner);
},
remove: function(userId, doc) {
return false;
},
download: function(userId) {
return true;
},
fetch: []
});
在这里您将找到另一个示例,单击
另一个错误点可能是您的AWS配置。您是否已完成此处编写的所有内容?
基于此后单击,似乎当FS.File()的构造不正确时,会发生此错误。因此,也许这应该是您入门的第一条方法。
很多阅读,所以我希望这对您有帮助:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句