1

I am trying to convert a csv file to a JSON using angular JS. I am taking the CSV file as an input from the user. My html code is as follows

 <div class="input-group input-group-sm" id="fileUploader">
        <span class="input-group-btn">
            <span class="btn btn-primary btn-file">
                Browse<input type="file" ngf-select ng-model="csvFile" name="file" accept=".csv"></input>
            </span>
        </span>         
        <input type="text" class="form-control" placeholder="Choose file" value={{csvFile.name}} readonly></input>
        <span class="input-group-btn">
            <button class="btn btn-primary" type="submit" ng-click="onFileSelect(csvFile)">Ok</button>
        </span>
    </div>

and my controller is as follows

  $scope.onFileSelect = function($scope, $http,Upload) 
    {
      console.log("Hello from login controller");
        $scope.onFileSelect = function(file) {
            if (angular.isArray(file)) {
                file = file[0];
            }
            if (file.type !== 'text/csv'){
                alert('Only CSV are accepted.');
                return;
            }
            //want to convert the file to JSON.  
        };
}

I followed the example in this link JS Fiddle

In that example he was taking input as follows

<textarea id="csv" class="text">
    "Id","UserName"
    "1","Sam Smith"
    "2","Fred Frankly"
    "1","Zachary Zupers"
</textarea>

and in his controller he is doing the following

var csv = $("#csv").val();
var json = CSV2JSON(csv);

But I have a csv File as shown in the image

enter image description here

Can someone tell me on how to convert the file variable that I have in my controller to the format accepted by the CSV2JSON function in the example.

Ranger
  • 141
  • 1
  • 11
  • Possible duplicate of [How to load CSV file to object in angualrjs?](http://stackoverflow.com/questions/16933711/how-to-load-csv-file-to-object-in-angualrjs) – Amir Apr 08 '16 at 13:39
  • Please see the edited version of the question – Ranger Apr 08 '16 at 14:04
  • These questions might help: [Reading files with JavaScript FileReader](http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded) and [AngularJS check for file input changes](http://stackoverflow.com/questions/17922557/angularjs-how-to-check-for-changes-in-file-input-fields) – Shaun Apr 08 '16 at 14:53

0 Answers0