How to read csv file content in AngularJS?

本小妞迷上赌 提交于 2019-11-27 01:05:50

问题


I want to read a csv file and get its content by using AngularJS and HTML5. I want to put the content of the csv file in $scope.

I have this code in my html

 <table align="center" >
            <tr class="borderless">
                <td>Choose .csv file</td>
                <td><input type="file" name="readCsvFile" id="readCsvFile" accept=".csv"> </td>
                <td><input type="button" value="Upload" ng-click="uploadCsvFile()"></td>
            </tr>
 </table>

回答1:


with a custom directive:

app.directive('fileReader', function() {
  return {
    scope: {
      fileReader:"="
    },
    link: function(scope, element) {
      $(element).on('change', function(changeEvent) {
        var files = changeEvent.target.files;
        if (files.length) {
          var r = new FileReader();
          r.onload = function(e) {
              var contents = e.target.result;
              scope.$apply(function () {
                scope.fileReader = contents;
              });
          };

          r.readAsText(files[0]);
        }
      });
    }
  };
});

http://plnkr.co/edit/eeQbR65oE8iljm7ueGhX?p=preview



来源:https://stackoverflow.com/questions/26353676/how-to-read-csv-file-content-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!