I am trying to upload my file(s) on server once opened. I have a function in my service and calling that service to upload my file(s). This is my service function.
function post(file) {
var file = file;
if (angular.isDefined(file)) {
return Upload.upload({
url: filesConfig.uploadFile,
data: {
file: file
}
}).then(function (response) {
return response;
},
function (error) {
return error;
},
function (evt) {
var progress = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progress + '% ' + evt.config.data.file.name);
});
}
}
In last evt arg function I am getting progress in variable. I am calling this post function from controller like this.
angular.element(document.querySelector('#fileinput')).on('change', function () {
for (var i = 0; i < this.files.length; i++) {
vm.filess.push(this.files[i]);
fileUpload.post(vm.filess[i]).then(function (response) {
console.log(response);
});
}
});
Now I want to detect progress of each file in my controller, to show progress bar in UI. How to detect progress?
post(file, callback)and then execute callback on every progress change; or 2 - Use pub/sub to dispatch an event every time the progress changes and then subscribe to this event in your controller; or 3 - use a mediator/manager/service object that keeps reference to this progress and update it on every step and then inject this reference into your controller and play with it; or 4 - update reference forfilewith the progress value such asfile.progress = progressinside your service - not quite intuitive though