在angularJs中验证CSV文件

时间:2017-03-15 08:15:39

标签: angularjs

我有一个csv文件,其中包含两列store_id和store_name。当用户上传csv文件时,我需要验证文件,即检查第一列是否为Integer(store_id),第二列是否为String(store_name)。可以帮助如何阅读和检索内容并验证文件?

由于

3 个答案:

答案 0 :(得分:0)

document.getElementById('csvUpload').onchange = function(){
        var file = this.files[0];
        var storeId = [],storeName = [],line,flag = 1;
        var reader = new FileReader();
        reader.onload = function(progressEvent){
            var lines = this.result.split('\n');
            for(var iterator = 0; iterator < lines.length-1; iterator++){
                line = lines[iterator].split(',');
                storeId.push(line[0]);
                storeName.push(line[1]);
            }
            var result = !storeId.some(isNaN);
            for(var iterator = 0; iterator < storeName.length; iterator++) {
                console.log(typeof storeName[iterator]);
                if(typeof storeName[iterator] !== "string"){
                    flag = 0;
                    break;
                }
            }
            if(result === false || flag === 0) {
                var alert = mdDialog.alert({
                    textContent: 'Please select a valid csv file',
                    ok: 'Close'
                });
                mdDialog.show( alert );
            }
        };
        reader.readAsText(file);
    }

答案 1 :(得分:0)

试用此代码

我的csv文件

    store_id,store_name
01,"First point"
02,"Second point"
03,"Third point"

&#13;
&#13;
var demo = angular.module('demo', []);
demo.controller('loadData', function($scope,$rootScope,$http){
   $scope.uploadData = function() {
	   var uploaded=$scope.fileContent;
		$scope.processData(uploaded);
   };
   	$scope.processData = function(allData) {
		var filteredData = allData.split(/\r\n|\n/);
		var headers = filteredData[0].split(',');
		var final = [];

		for ( var i = 0; i < filteredData.length; i++) {

		if (!filteredData[i]=="") {

			var data = filteredData[i+1].split(',');

			if (isNaN(data[0])==false && isNaN(data[1])==true) {
				final.push(data);
				console.log("Valid CSV");
			}
			else{
				console.log("Not Valid CSV");
			}
		}
		}
		$scope.data = final;
	};
});
demo.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]);
        }
      });
    }
  };
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<div ng-app='demo'>
	<div ng-controller='loadData'>
	  <input type="file" file-reader="fileContent" />
	  <button ng-click='uploadData($event)' >upload</button>
      <div>{{fileContent}}</div>
	  <table>
		<tr ng-repeat="x in data">
		  <td ng-repeat="y in x">{{ y }}</td>
		</tr>
	  </table>
	</div>
	</div>
&#13;
&#13;
&#13;

答案 2 :(得分:-2)

你可以试试这个组件 https://www.import-javascript-angular-xlsx-csv-validator.com/ 它验证了这一点以及更多