javascript - Generate array from CSV file selected by input type file -


i want read csv file upload using input type file , feed data array.

i'm using angularjs input type file read csv, xls or xlsx file follows:

html:

<input class="btn btn-default col-xs-6" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" onchange="angular.element(this).scope().checkformat(this.files)"> 

javascript/angularjs:

$scope.checkformat = function(files) {         var fd = new formdata();         //take first selected file         fd.append("file", files[0]); } 

how can read csv file row row , push each row array?

the best way make directive such functionality (say call csvupload , use <csv-upload ng-model="mydata">) - way reusable , won't have implement logic in, possibly, many controllers. it's quite convenient too: once have choose file and, bam, have data on $scope.mydata :)

this how did it:

(to transform csv json i'm using quite estabilished https://github.com/mholt/papaparse library parse csv string json yourself. not recommend though ;)

.directive('csvupload', function () {   return {     restrict: 'e',     template: '<input type="file" onchange="angular.element(this).scope().handlefiles(this.files)">',     require: 'ngmodel',     scope: {},     link: function (scope, element, attrs, ngmodel) {       scope.handlefiles = function (files) {         papa.parse(files[0], {           dynamictyping: true,           complete: function(results) {             // can transform uploaded data here if necessary             // ...             ngmodel.$setviewvalue(results);           }         });       };      }   }; }); 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -