Reputation: 6387
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.
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?
Upvotes: 0
Views: 1865
Reputation: 16498
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);
});
}
}
};
});
Upvotes: 2