Skip to content

Instantly share code, notes, and snippets.

@ashishtilara
Last active October 14, 2016 00:35
Show Gist options
  • Save ashishtilara/f6952a353d691ed6f4e1c7403ea88a9b to your computer and use it in GitHub Desktop.
Save ashishtilara/f6952a353d691ed6f4e1c7403ea88a9b to your computer and use it in GitHub Desktop.
angular-csv-upload html5
var ctrl,
module;
module = ng.module("SYS.Directive.FileReader", [
"ng"
]);
ctrl = function ($parse) {
return {
restrict: "A",
scope: false,
link: function (scope, element, attrs) {
var fn = $parse(attrs.fileReader);
element.on("change", function (onChangeEvent) {
var reader = new FileReader();
reader.onload = function (onLoadEvent) {
scope.$apply(function () {
fn(scope, {$fileContent: onLoadEvent.target.result});
});
};
reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
});
}
};
};
ctrl.prototype.constructor = ctrl;
module.directive("fileReader", ctrl);
ctrl.$inject = ["$parse"];
return module;
//HTML
//<input type="file" title="Choose File" file-reader="ctrl.showContent($fileContent)" accept=".csv" />
/*
//Controller Function
ctrl.prototype.showContent = function ($fileContent) {
this.content = $fileContent;
};
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment