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
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.