-1

I am trying to read a csv file in angular js and i got through this question How to read csv file content in angular js? where it is used as directive.But i am submitting it as a form in my controler ,so how can i use in my form.Can anyone suggest help.Thanks.

My code,

  if($scope.partner.csv){
          var files = $scope.partner.csv.target.files
           var r = new FileReader();
           r.onload = function(e) {
               var contents = e.target.result;
                $scope.$apply(function () {
                 $scope.fileReader = contents;
               });
          };
        }
      }

This is what i had tried,when i submit my form if csv file is uploaded the above action should be done.

9
  • Please provide more specifics about exactly what you are trying to accomplish how this should work Commented Jan 21, 2017 at 14:00
  • what does "submittiong it as a form in my controoler" mean? not just the fact that the english is wrong; how does a CSV file relate to a form? Commented Jan 21, 2017 at 14:00
  • Is it useful if you create an array from your CSV file? Commented Jan 21, 2017 at 14:04
  • Hi Randy exactly i am tryint to do that. Commented Jan 21, 2017 at 14:09
  • the answer also explains your requirement. stackoverflow.com/questions/26353676/… Commented Jan 21, 2017 at 14:19

1 Answer 1

3

You can use the below code to read csv file.

<!DOCTYPE html>
<html ng-app="" ng-controller="myCtrl">
<style>
table, th, td {
   border: 1px solid black;
   padding:5px;
}
table {
   border-collapse: collapse;
   margin:10px;
}
</style>
<body>
<button ng-click="readCSV()">
Display CSV as Data Table
</button>
<div id="divID">
  <table>
    <tr ng-repeat="x in data">
      <td ng-repeat="y in x">{{ y }}</td>
    </tr>
  </table>
</div>
<div>
<table>
</table>
</div>
<script>
function myCtrl($scope, $http) {
    $scope.readCSV = function() {
        // http get request to read CSV file content
        $http.get('/angular/sample.csv').success($scope.processData);
    };

    $scope.processData = function(allText) {
        // split content based on new line
        var allTextLines = allText.split(/\r\n|\n/);
        var headers = allTextLines[0].split(',');
        var lines = [];

        for ( var i = 0; i < allTextLines.length; i++) {
            // split content based on comma
            var data = allTextLines[i].split(',');
            if (data.length == headers.length) {
                var tarr = [];
                for ( var j = 0; j < headers.length; j++) {
                    tarr.push(data[j]);
                }
                lines.push(tarr);
            }
        }
        $scope.data = lines;
    };
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.