Skip to content

Instantly share code, notes, and snippets.

@mir4ef
Last active June 30, 2016 11:45
Show Gist options
  • Save mir4ef/6c5f22f355c28ffc8c17fd4b7d14426a to your computer and use it in GitHub Desktop.
Save mir4ef/6c5f22f355c28ffc8c17fd4b7d14426a to your computer and use it in GitHub Desktop.
Capture a file from input type='file' with AngularJS
/**
* @file file-input.component.js
* @author Miroslav Georgiev
* @version 0.0.1
*/
(function () {
'use strict';
/**
* @description Controller to read and upload the specific element
* @param $element
*/
function FileReadCtrl($element) {
var vm = this;
vm.handleFile = function (changeEvent) {
var reader = new FileReader();
reader.onload = function (loadEvent) {
console.log('file content:', loadEvent.target.result);
};
reader.readAsDataURL(changeEvent.target.files[0]);
};
$element.bind('change', vm.handleFile);
}
/**
* @description Object containing the options for the scroll component
*/
var fileReader = {
bindings: {},
controller: 'FileReadCtrl as file',
require: '?ngModel',
template: '<input type="file" />'
};
// we need to inject the controller, because of strict DI
angular.module('core').controller('FileReadCtrl', FileReadCtrl);
angular.module('core').component('fileReader', fileReader);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment