0

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?

4

1 回答 1

2

由于 $scope.files 是数组,您需要将 $watch 函数的第三个参数设置为 'true'

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

请在此处查看工作演示

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);
        });
      }
    }
  };
});
于 2015-02-06T12:41:14.180 回答