2015-09-06 48 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的工作。它在我目前的項目中非常實用。 –