Skip to content

Instantly share code, notes, and snippets.

@garrypolley
Created November 18, 2015 22:03
Show Gist options
  • Save garrypolley/2616820361dcaf80d321 to your computer and use it in GitHub Desktop.
Save garrypolley/2616820361dcaf80d321 to your computer and use it in GitHub Desktop.
Simple file upload for Angular
<upload-file upload-url="http://google.com/">
</upload-file>
UploadFile.$inject = ['$parse', '$http'];
function UploadFile($parse, $http) {
var fileToUpload;
return {
restrict: 'E',
scope: {
uploadUrl: '='
},
template: '<input type="file">',
link: function(scope, element) {
element.bind('change', function() {
var fileToUpload = element.children()[0].files[0],
formData = new FormData();
formData.append('file', file);
$http.post(scope.uploadUrl, formData, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
console.log('Successful file upload');
element.unbind('change');
})
.error(function(){
console.log('Failed to upload file');
});
});
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment