Skip to content

Instantly share code, notes, and snippets.

@twilson63
Last active December 16, 2015 17:39
Show Gist options
  • Save twilson63/5471450 to your computer and use it in GitHub Desktop.
Save twilson63/5471450 to your computer and use it in GitHub Desktop.
app.directive('uploadButton', function($parse, $compile) {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<span class="upload-button {{class}}">' +
'<span ng-transclude></span>' +
'<input type="file">' +
'</span>',
link: function(scope, element, attrs) {
element.find('input').bind('change', function() {
var fd = new FormData();
fd.append('uploadFile', this.files[0]);
var xhr = new XMLHttpRequest();
// xhr.upload.addEventListener("progress", function(e) {
// var fn = $parse($scope.progress);
// $scope.$apply(function () {
// if (fn) { fn($scope, { $event: e }); }
// });
// }, false);
xhr.addEventListener("load", function(e) {
var fn = $parse(attrs.complete);
scope.$apply(function () {
if(fn) { fn(scope, { $data: xhr.responseText, $status: xhr.status }); }
});
}, false);
xhr.open("POST", attrs.action);
xhr.send(fd);
});
}
};
});
.upload-button {
position: relative;
overflow: hidden;
margin-right: 4px;
}
.upload-button input {
position: absolute;
top: 0;
right: 0;
margin: 0;
opacity: 0;
filter: alpha(opacity=0);
transform: translate(-300px, 0) scale(4);
font-size: 23px;
direction: ltr;
cursor: pointer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment