angular-file-upload is not posting anything

Issue

I am trying to use angular-file-upload. The file is being sent from the view to the angular controller but it is not sending anything to the apiController. I have made a plunker.

Plunker

It drops the file at

$scope.upload = function (files) {
      $scope.$watch('files', function () {
    $scope.upload($scope.files);
});

$scope.upload = function (files) {
    if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            $upload.upload({
                url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
                fields: { 'companyName': $scope.companyName },
                file: file
            }).progress(function (evt) {
                var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
            }).success(function (data, status, headers, config) {
                console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
            });
        }
    }
};

Update

I see how your success function is being hit. mine still is not. and there is no javascript errors in my console. what can i do to debug it?

Solution

As $scope.files is array you need to set third argument of $watch function to ‘true’

   $scope.$watch('files', function () {
      console.log($scope.files);
        $scope.upload($scope.files);
    }, true);

please see working demo here

http://plnkr.co/edit/lGjgTIeVZdgxcS2kaE7p?p=preview

app.controller('MainCtrl', function($scope, $upload) {
  $scope.$watch('files', function() {
    $scope.upload($scope.files);
  });

  $scope.upload = function(files) {
    if (files && files.length) {
      for (var i = 0; i < files.length; i++) {
        var file = files[i];
        $upload.upload({
          url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
          fields: {
            'companyName': $scope.companyName
          },
          file: file
        }).progress(function(evt) {
          var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
          console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
        }).success(function(data, status, headers, config) {
          console.log('file ' + config.file.name + 'uploaded. Response: ');
         //response from server          
         console.log(data);
        });
      }
    }
  };
});

Answered By – sylwester

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.