2
var app = angular.module('myngCsv', [ngcsv]);
app.controller('ngcsvCtrl', function($scope,$csv) {

    $scope.csv ={
        content: null;
        header: true,
        headerVisible: true,
        separator: ',',
        separatorVisible: true,
        result: null,
        encoding: 'ISO-8859-1',
        encodingVisible: true,
        accept: ".csv"
    };

});

This is what tried. indeed.

3 Answers 3

3

You can write a function to convert CSV to JSON and pass your CSV object to it. All you need to do is to save the first row of CSV as headers (You can identify them by comma) and you can identify new entry by newline character \n Go through the function I have written below.

function csvToJSON(csv, callback) {
            var lines = csv.split("\n");
            var result = [];
            var headers = lines[0].split(",");
            for (var i = 1; i < lines.length - 1; i++) {
                var obj = {};
                var currentline = lines[i].split(",");
                for (var j = 0; j < headers.length; j++) {
                    obj[headers[j]] = currentline[j];
                }
                result.push(obj);
            }
            if (callback && (typeof callback === 'function')) {
                return callback(result);
            }
            return result;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply, i'll try.
1

You can try using Papaparse looks very easy and elegant to use

1 Comment

Thanks for reply, i'll try
0

Create fileReader directive:

<input type="file" data-file-reader-directive="fileContent" accept=".csv" />

Directive to get csv data from the file:

app.directive('fileReaderDirective', function() {
    return {
        restrict: "A",
        scope: {
            fileReaderDirective: "=",
        },
        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.fileReaderDirective = contents;
                        });
                    };
                    r.readAsText(files[0]);
                }
            });
        }
    };
});

Create a factory to convert csv data to json data

app.factory('readFileData', function() {
    return {
        processData: function(csv_data) {
            var record = csv_data.split(/\r\n|\n/);
            var headers = record[0].split(',');
            var lines = [];
            var json = {};

            for (var i = 0; i < record.length; i++) {
                var data = record[i].split(',');
                if (data.length == headers.length) {
                    var tarr = [];
                    for (var j = 0; j < headers.length; j++) {
                        tarr.push(data[j]);
                    }
                    lines.push(tarr);
                }
            }

            for (var k = 0; k < lines.length; ++k){
              json[k] = lines[k];
            }
            return json;
        }
    };
});

check out the working example : https://plnkr.co/edit/ml29G85knZpWWNdG8TeT?p=preview

1 Comment

Thanks for reply, i'll try

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.