Skip to content

Instantly share code, notes, and snippets.

@PabloPG
Last active May 23, 2016 12:57
Show Gist options
  • Save PabloPG/a4f3b1ef333034b78535c62ef25d06a9 to your computer and use it in GitHub Desktop.
Save PabloPG/a4f3b1ef333034b78535c62ef25d06a9 to your computer and use it in GitHub Desktop.
// Envia os dados com o upload
$http({
method: 'POST',
url: ,
headers: { 'Content-Type': undefined },
transformRequest: function(data) {
var formData = new FormData();
formData.append("post", angular.toJson(data.post));
// Agora add os arquivos
for (var i = 0; i < data.files.length; i++) {
formData.append("file" +i, data.files[i]);
}
return formData;
},
data: {post: $scope.post, files: $scope.files}
})
.success(function( data, status, headers, config) {
})
/* Diretiva */
(function() {
'use strict';
angular
.module('app')
.directive('fileUpload', fileUpload);
function fileUpload() {
return {
scope: true, //create a new scope
link: function (scope, el, attrs) {
el.bind('change', function (event) {
var files = event.target.files;
//iterate files since 'multiple' may be specified on the element
for (var i = 0;i<files.length;i++) {
//emit event upward
scope.$emit("fileSelected", { file: files[i] });
}
});
}
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment