Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created June 12, 2014 20:10
Show Gist options
  • Save kentcdodds/fa18dacf764095718bb8 to your computer and use it in GitHub Desktop.
Save kentcdodds/fa18dacf764095718bb8 to your computer and use it in GitHub Desktop.
kcdBase64
angular.module('app').directive('kcdBase64', function () {
'use strict';
return {
restrict: 'A',
template: [
'<span class="button file-upload-button">',
'<span>Select Image</span>',
'<input type="file" class="input-file" accept="image/*">',
'</span>'
].join(''),
scope: {
onBase64Loaded: '&kcdBase64'
},
link: function (scope, el) {
el.find('input').on('change', function() {
if (this.files && this.files[0]) {
// Base64 file
var FR = new FileReader();
FR.onload = function(e) {
scope.onBase64Loaded({base64:e.target.result});
scope.$root.safeApply();
};
FR.readAsDataURL(this.files[0]);
}
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment