Created
October 12, 2016 06:24
-
-
Save fercreek/1cb7038a3122e8db637acda79eb087a4 to your computer and use it in GitHub Desktop.
Update multiple files from AngularJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('starter.controllers', []) | |
.controller('TestCtrl', TestCtrl); | |
TestCtrl.$inject = ['updateService']; | |
function TestCtrl(updateService){ | |
var vm = this; | |
vm.addImage = addImage; | |
vm.data = {}; | |
function addImage() { | |
var file = vm.data.myFile; | |
console.log('file is '); | |
console.dir(file); | |
updateService.uploadFiles(file); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('starter', []) | |
.directive('fileModel', ['$parse', function ($parse) { | |
return { | |
restrict: 'EA', | |
link: function(scope, element, attrs) { | |
var model = $parse(attrs.fileModel); | |
var isMultiple = attrs.multiple; | |
var modelSetter = model.assign; | |
element.bind('change', function(){ | |
var values = []; | |
angular.forEach(element[0].files, function (item) { | |
// var value = { | |
// // File Name | |
// name: item.name, | |
// //File Size | |
// size: item.size, | |
// //File URL to view | |
// url: URL.createObjectURL(item), | |
// // File Input Value | |
// _file: item | |
// }; | |
values.push(item); | |
}); | |
scope.$apply(function () { | |
if (isMultiple) { | |
modelSetter(scope, values); | |
} else { | |
modelSetter(scope, values[0]); | |
} | |
}); | |
}); | |
} | |
}; | |
}]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('starter.services', []) | |
.factory('MantenimientosService', MantenimientosService); | |
MantenimientosService.$inject = ['BASE_URL', '$http']; | |
function MantenimientosService(BASE_URL, $http) { | |
return { | |
uploadFiles: uploadFiles | |
}; | |
function uploadFiles(file) { | |
var fd = new FormData(); | |
for (var key in file) { | |
fd.append('file', file[key]); | |
} | |
var url = BASE_URL + '/uploadPhotos'; | |
$http.post(url, fd, { | |
transformRequest: angular.identity, | |
headers: {'Content-Type': undefined}, | |
withCredentials: false | |
}) | |
.then(log) | |
.catch(log); | |
function log(data){ | |
console.log(data); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form> | |
<input type="file" file-model="vm.data.myFile" multiple> | |
<button type="button" class="button button-energized" ng-click="vm.addImage()"> | |
Add image | |
</button> | |
</form> | |
{{vm.data}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment