其实FSCollection有一个名为Meteor-cfs-ui的Package,并且有一个进度条。
所以首先添加它
meteor add cfs:ui
所以有了这个包,你可以在<template>上做这个,这只是一个HTML解决方案
{{#each images}}
{{#unless this.isUploaded}}
{{> FS.UploadProgressBar}}
{{/unless}}
{{/each}}
如果您不想在 HTML 端进行工作,还有一个 JavaScript 解决方案,有 2 个可能的选项客户端或服务器端,选择您认为更适合的。
客户端解决方案,这里有 2 个方法 fileObj.isUploaded 和 fileObj.hasStored(README 上没有关于此的文档,因为包的创建者几天前清理了它。 )
var fileId; //store the current id of the file
$scope.upload = function(file) {
Images.insert(file._file, function (err, fileObj) {
if (err) console.log(err);
else {
fileId = result._id //get the id of the upload file
$scope.project.teaserImg = "/cfs/files/images/" + fileObj._id;
$scope.$apply(); //Force the refresh
}
});
};
//Some Reactive tracker to check when the file its ready
Tracker.autorun(function (computation) {
var fileObj = Images.findOne(fileId);
if (fileObj.hasStored('yourStoreName')) {
computation.stop();
}
});
或服务器端解决方案,CFS在服务器端有3个事件,stored,uploaded和error。
Images.on('stored', function (fileObj, storeName) {
console.log("The " + fileObj + " with the _id " + fileObj._id " + just get stored")
});
Images.on('uploaded', function (fileObj) {
console.log("The " + fileObj + " with the _id " + fileObj._id " + just get uploaded")
});
上传用户解决方案
var fileId; //store the id of the uploaded file
$scope.upload = function(file){
Images.insert(file._file, function (err,fileObj) {
if (err) console.log(err);
else { fileId = fileObj._id
}
});
};
和追踪器
Tracker.autorun(function (computation) {
var fileObj = Images.findOne(fileId);
if (fileObj) { $scope.project.teaserImg = "/cfs/files/images/" + fileId; $scope.$apply();
computation.stop();
}
});