I have a form and a few of that form fields are file uploads. This is what I have:
- User fills the form up
- User selects the files to submit
- User presses submit
Now, this is what I want to do:
- Post the form to server, getting back an ID
- Post file one to server myresource/ID/fileone
- Post file two to server myresource/ID/filetwo ...
¿How can I perform this files upload programatically? (I'm using angular promises, so no problem with sequential requests...)
Here is my code:
$scope.upload = function (files, url) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
Upload.upload({
url: url,
//fields: {'username': $scope.username},
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);
});
}
}
};
My html:
<input type="file" class="btn btn-danger" ng-file-select ng-model="files" ng-multiple="multiple"> Doit!
<input class="btn btn-danger" ng-file-select ng-model="files" ng-multiple="multiple">Doit too!