【发布时间】:2015-06-01 12:53:06
【问题描述】:
我正在使用带有 S3 适配器的 CollectionFS 包,我查看了一些不同的解决方案,但无法使其正常工作。
问题:即使文件/图像已成功上传到 S3,但在安全显示图像之前触发了成功上传的回调。这有时会导致显示损坏的图像。
我发现了fileObj.once("uploaded", function(){}) 回调,但似乎“已上传”基本上意味着将图像发送到服务器。到那时 S3 上传不会发生。我发现的一个临时解决方法是让setTimeout 持续 3-4 秒,但这并不可靠。
这是我的上传代码:
FS.Utility.eachFile(event, function(file) {
Session.set('profilePhotoUploaded', false);
var newFile = new FS.File(file);
newFile.metadata = {owner: Meteor.userId()};
ProfileImages.insert(newFile, function (err, fileObj) {
if (err){
console.log("error! - " + err);
} else {
// handle success depending what you need to do
var userId = Meteor.userId();
// This does NOT run when image is stored in S3. I think it runs when the image reached the app server.
fileObj.once("uploaded", function () {
// timeout of 3 seconds to make sure image is ready to be displayed
// --- This is not a good solution and it image does is not always ready
setTimeout(function(){
var uploadedImage = {
"profile.image.url": "/cfs/files/profileImages/" + fileObj._id
};
Meteor.users.update(userId, {$set: uploadedImage});
Session.set('profilePhotoUploaded', true);
}, 3000);
console.log("Done uploading!");
});
}
});
});
是否有不同的回调来检查图像是否实际存储在 S3 中?我试过fileObj.once("stored", function(){}) 但这不起作用。
【问题讨论】:
标签: javascript file-upload meteor amazon-s3