1

I have a function like this:

$scope.fileUpload = function (files) {
                angular.forEach(files, function () {
                    var fd = new FormData();
                    fd.append('files[]', files[0]);

                    $http.post('/my-url', fd, {
                        transformRequest: angular.identity,
                        headers: {
                            'Content-Type': undefined}
                    }).then(function (data) {
                        console.log(data);
                    });
                });
            };

and template:

<input type="file"
               name="files[]"
               id="fileUploaderButton_contentFormFiles"
               onchange="angular.element(this).scope().fileUpload(files)"
               multiple>
<div class="progress" id="fileUploaderProgressbar_contentFormFiles">
    <div class="progress-bar progress-bar-striped active"
         role="progressbar"
         aria-valuenow="{{progress}}"
         aria-valuemin="0"
         aria-valuemax="100"
         style="min-width: 2em; width: {{progress}}%">
        <span>{{progress}}%</span>
    </div>
</div>

How can I $watch for for transferred value (bytes) of an uploading file so I can update {{progress}} in the progress bar during $http request (using native angular features)? Thanks in advance.

1

3 Answers 3

2

Not the answer you want but your approach is wrong

You should be using ng-file-upload which is the standard angular approach to this and supports upload progress tracking

Sign up to request clarification or add additional context in comments.

2 Comments

Since when ng-file-upload is a standard approach?
Yes, ng-file-upload has many features. Do take a look.
1

thank you all very much for your advices. My solution is to use XMLHttpRequest instead of $http service:

$scope.fileUpload = function (files) {
                angular.forEach(files, function () {
                    var fd = new FormData();
                    fd.append('files[]', files[0]);

                    var xhr = new XMLHttpRequest();

                    xhr.upload.onprogress = function (event) {
                        $scope.progress = Math.floor((event.loaded / event.total) * 100);
                        console.log(event.loaded + ' / ' + event.total);
                    }

                    xhr.open("POST", '/my-url', true);
                    xhr.send(fd);
                });
            };

1 Comment

This is very non angular but if u don't care the. Go for it
1

You can use the ng-file-upload directive. It supports drag&drop, file progress/abort and file upload for non-HTML5 browsers.

HTML:

<div ng-controller="MyCtrl">
  <input type="file" ng-file-select="onFileSelect($files)" multiple>
</div>

JS:

//make sure you inject angularfileupload
angular.module('myApp', ['angularFileUpload']);

var MyCtrl = [ '$scope', '$upload', function($scope, $upload) {
  $scope.onFileSelect = function($files) {
    //$files: an array of files selected, each file has name, size, and type.
    for (var i = 0; i < $files.length; i++) {
      var $file = $files[i];
      $upload.upload({
        url: 'my/upload/url',
        file: $file,
        progress: function(e){}
      }).then(function(data, status, headers, config) {
        // file is uploaded successfully
        console.log(data);
      }); 
    }
  }
}];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.