JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script>
<script src="https://angular-file-upload.appspot.com/js/ng-file-upload-shim.js"></script>
<script src="https://angular-file-upload.appspot.com/js/ng-file-upload.js"></script>
<body ng-app="fileUpload" ng-controller="MyCtrl">
<h4>Upload on file select</h4>
<button ngf-select="saveFiles($files, $invalidFiles)" multiple accept="image/*" ngf-max-height="1000" ngf-max-size="1MB">
Select Files</button>
<button ng-click="uploadFiles()">
Upload one by one
</button>
<br>
<br> Files:
<ul>
<li ng-repeat="f in files" style="font:smaller">{{f.name}} {{f.$errorParam}}
<span class="progress" ng-show="f.progress >= 0">
<div style="width:{{f.progress}}%"
ng-bind="f.progress + '%'"></div>
</span>
</li>
<li ng-repeat="f in errFiles" style="font:smaller">{{f.name}} {{f.$error}} {{f.$errorParam}}
</li>
</ul>
{{errorMsg}}
</body>
CSS
.thumb {
width: 24px;
height: 24px;
float: none;
position: relative;
top: 7px;
}
form .progress {
line-height: 15px;
}
}
.progress {
display: inline-block;
width: 100px;
border: 3px groove #CCC;
}
.progress div {
font-size: smaller;
background: orange;
width: 0;
}
JavaScript
//inject angular file upload directives and services.
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', '$q', function($scope, Upload, $timeout, $q) {
var fileList = [];
$scope.files = [];
$scope.errFiles = [];
var item = {}; //just a dummy object since I don't have your code
$scope.saveFiles = function(files, errFiles) {
$scope.files = files;
$scope.errFiles = errFiles;
}
$scope.uploadFiles = function() {
fileList = [];
if ($scope.files <= 0) {
//validation logic
return;
} else {
angular.forEach($scope.files, function(file) {
fileList.push({
key: file,
item: {}
})
});
debugger;
fileList[0].item = item;
sequentialFileUploadWithReduce(fileList)
.then(function() {
//cleanup code
$scope.files = [];
$scope.errFiles = [];
});//or Error handler on catch
}
};
function sequentialFileUploadWithReduce(values) {
var result = ''
var dfd = $q.defer();
dfd.resolve();
return values.reduce(function(currentValue, nextValue, index, values) {
return Upload.upload({
url: "https://angular-file-upload-cors-srv.appspot.com/upload",
method: "POST",
data: currentValue
}).then(function(resp) {
// file is uploaded successfully
//debugger;
result += "Uploading file " + index++;
console.log("Uploading file " + index);
nextValue.item = resp;
}, function(resp) {
// handle error
}, function(evt) {
// progress notify
});
}, dfd.promise).then(function(responce) {
return result;
});
}
}]);