Search code examples
javascriptangularjsfile-uploadangular-file-upload

Sending intermediate data from service to controller using angular-file-upload


I'm using angular-file-upload available here: https://github.com/danialfarid/angular-file-upload. As you can see in the example furnished on the same page, file data is posted to backend from a controller using the directives made available.

When I implemented the code however I moved the file posting to a service as that would allow me to re-use the functionality & feels the correct way(disagree?). Everything works fine except that I'm unable to figure out how to send the data from the .progress(function(evt)){}... function that tracks file upload completion status to the controller and respectively echo it on the html page.

Using the .then I can get the final success response I guess but how about this progress data?

Am I missing something very simple here? How to get this done?


Solution

  • After a longtime, here is how this would work.

    Using the notify callback for the promise solves the problem. As I had stated earlier, I had moved the file upload code (based on this rep enter link description here) into the angular services and I was not able to get file upload progress. Since I was returning the promise from services, so I could only get the file completion.

    the above git rep also has a

    .progress(function(evt){ file_upload_percent=(parseInt(100.0 * evt.loaded / evt.total)})
    

    function that gives the file completion percentage. Using the promise notify here, we can return this intermediate value like this :

    .progress(function(evt) {   file_upload_percent=(parseInt(100.0 * evt.loaded / evt.total)); deferred.notify(file_upload_percent);})
    

    later using notify callback from the promise return, we can access the file completion percentage.

    .then(function(response){},function(){},function(update){$scope.file_upload_percent=update});
    

    use $scope.file_upload_percent as you would wish to.