2015-09-06 50 views
2

我有一个控制器:角算法:停止ngFileUpload双循环?

这基本上是被用来允许通过 ngFileUpload csv文件的拖动n个墨滴,然后通过 Papa Parse解析
app.controller('FileUploadController', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) { 
    $scope.$watch('files', function() { 
     $scope.upload($scope.files); 
    }); 

    $scope.$watch('file', function() { 
     if ($scope.files !== null) { 
      $scope.upload([$scope.file]); 
     } 
    }); 

    $scope.upload = function (files) { 
     $scope.log = ''; 
     if (files && files.length) { 
      $scope.numberOfFiles = files.length; 
      for (var i = 0; i < files.length; i++) { 
       var file = files[i]; 
       if (file && !file.$error) { 
        Upload.upload({ 
         url: 'http://localhost/Reconcile/index.php', 
         file: file 
         // fileName: i // to modify the name of the file(s) 
        }).progress(function (evt) { 
         var progressPercentage = parseInt(100.0 * evt.loaded/evt.total); 
         $scope.log = progressPercentage; 
        }).success(function (data, status, headers, config) { 
         for (var j = 0; j < 2; j++) { 
          $scope.parsePapa(files[j], j); 
         } 
        }).error(function (data, status, headers, config) { 
         $scope.log = 'error status: ' + status; 
        }); 
       } 
      } 
     } 
    }; 
}]); 

。在我对Papa Parse(位于$ scope.parsePapa)的调用中,我需要第二个参数来发送关于哪个文件上传(仅索引很好)的信息以供组织之后使用,第一个参数是文件本身。问题是,如果成功的功能,我做了这样的事情:

}).success(function (data, status, headers, config) { 
    $scope.parsePapa(files[i], i); 
} 

会发生什么事是success只调用一次两个文件都上传完毕。到那时,i已经在2(在2个文件的情况下:第一次通过0,第二个文件1,第二个文件后面2)。如果我使用上面的代码,它不返回文件,因为如果上传两个文件,files[2]的索引没有任何存储在其中的文件。

为了解决这个问题,我在for循环中使用了一个循环,它将循环遍历files的长度并打印出files中包含的每个file。但是,由于将要存在多个文件,因此当前的行为实际上(例如在上传2个文件的情况下)来解析每个文件两次。我正在解析具有1,000,000行的csv文件(稍后进行比较),所以上传两次会影响性能。

总之我正在寻找一种方法来发送每个文件$ scope.parsePapa(file,fileNumber),只有一次。

任何帮助将不胜感激。我对Angular完全陌生,所以如果这是我错过的简单事情,请提前致歉。


编辑:danial解决了这个问题(见下文)。如果任何人有兴趣,最终的代码看起来像这样:

app.controller('FileUploadController', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) { 
$scope.$watch('files', function() { 
    $scope.upload($scope.files); 
}); 

function uploadFile(file, index) { 
    Upload.upload({ 
     url: 'http://localhost/Reconcile/index.php', 
     file: file 
     // fileName: i // to modify the name of the file(s) 
    }).progress(function (evt) { 
     var progressPercentage = parseInt(100.0 * evt.loaded/evt.total); 
     $scope.log = progressPercentage; 
    }).success(function (data, status, headers, config) { 
     $scope.parsePapa(file, index); 
    }).error(function (data, status, headers, config) { 
     $scope.log = 'error status: ' + status; 
    }); 
} 

$scope.upload = function (files) { 
    $scope.log = ''; 
    if (files && files.length) { 
     $scope.numberOfFiles = files.length; 
     for (var i = 0; i < files.length; i++) { 
      var file = files[i]; 
      if (file && !file.$error) { 
       uploadFile(file, i); 
      } 
     } 
    } 
}; 

}]);

回答

1

如果你想知道上传文件的索引,你可以这样做:

function uploadFile(file, index) { 
    Upload.upload({....}).success(function() { 
     $scope.parsePapa(file, index); 
    })...; 
} 

$scope.upload = function (files) { 
    if (files && files.length) { 
     var file = files[i]; 
     if (file && !file.$error) { 
      uploadFile(file, i); 
     } 
    } 
} 

你也只需要这两个表款之一,如果你允许多个文件中的第一个就够了:

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

$scope.$watch('file', function() { 
    if ($scope.files !== null) { 
     $scope.upload([$scope.file]); 
    } 
}); 
+0

WOW!谢谢danial!我非常感谢你使用ng-file-upload的工作。它在我目前的项目中非常实用。 –