javascript - How to read a csv file in angularjs - Stack Overflow

I am trying to read a csv file in angular js and i got through this question How to read csv file conte

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.

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.

Share Improve this question edited May 23, 2017 at 11:46 CommunityBot 11 silver badge asked Jan 21, 2017 at 13:56 MMRMMR 3,03916 gold badges58 silver badges111 bronze badges 9
  • Please provide more specifics about exactly what you are trying to acplish how this should work – charlietfl 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? – Claies Commented Jan 21, 2017 at 14:00
  • Is it useful if you create an array from your CSV file? – Randy Commented Jan 21, 2017 at 14:04
  • Hi Randy exactly i am tryint to do that. – MMR Commented Jan 21, 2017 at 14:09
  • the answer also explains your requirement. stackoverflow./questions/26353676/… – Aniruddha Das Commented Jan 21, 2017 at 14:19
 |  Show 4 more ments

1 Answer 1

Reset to default 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 ma
            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./ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745477886a4629429.html

相关推荐

  • javascript - How to read a csv file in angularjs - Stack Overflow

    I am trying to read a csv file in angular js and i got through this question How to read csv file conte

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信