$http upload file progress in AngularJS

后端 未结 6 1692
南笙
南笙 2020-12-04 22:19

How can I get a \'progress\' event from my AngularJS $http POST request that is uploading an image? Is it possible to do this client-side, or do I need the server to report

6条回答
  •  死守一世寂寞
    2020-12-04 22:42

    I don't think Angular has something built-in to handle uploads.

    I think your best bet is to use something like jQuery File Upload. An idea for a solution would to create a Service that returns {progress:0} as default and then inside itself, implements the jQuery File Upload's progress update callback, which then simply keeps updating the progress. Thanks to Angular's binding, the upload progress would be in sync.

    angular.module('myApp.services', [])
      .factory('Uploader', function() {
      var uploaderService = {};
    
      var status = { progress: 0 };
    
      uploaderService.upload = function(inputEl) {
        inputEl.fileupload({
          /* ... */
          progressall: function (e, data) {
            status.progress = parseInt(data.loaded / data.total * 100, 10);
          }
        });
      };
    
      return uploaderService;
    });
    

提交回复
热议问题