Skip to content

Instantly share code, notes, and snippets.

@luismasuelli
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luismasuelli/cd67ff4c790ae8419283 to your computer and use it in GitHub Desktop.
Save luismasuelli/cd67ff4c790ae8419283 to your computer and use it in GitHub Desktop.
Angular File Upload - Crea la directiva file-model con soporte para envio de multipart y dependencia de jqueryfilestyle. La directiva ng-model NO banca file uploads
(function(){
angular
.module('FileUploads', [])
.factory('FileUploads.FormDataBuilder', ['$window', function(window) {
return function(data) {
var fd = new window.FormData();
angular.forEach(data, function(value, key) {
console.log(value);
fd.append(key, value);
});
return {
data: fd,
opts: {
transformRequest: angular.identity,
headers: {
'Content-Type': undefined
}
}
};
}
}]);
.directive('jfilestyle', function(){
return {
restrict: 'A',
scope: {
options: '=jfilestyle'
},
controller: ['$scope', function($scope){ /* vacio, para tirar error */ }],
link: function(scope, element, attrs/*, controller(s)*/) {
angular.element(element).jfilestyle(scope.options);
}
}
})
.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
require: 'jfilestyle',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
scope.$watch(attrs.fileModel, function(value){
if (!value) {
element.jfilestyle('clear');
}
});
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment