Read local csv file in Angular2

Reading local csv file in Angular2.

Read local csv file in Angular2 with example. upload .csv file in javascript. Display CSV or text file using JavaScript, JQuery, Bootstrap and Angular2.

We will Read, Parse and Display .csv file records in Angular2.

Download the sample application from below link,

Github page:
Download Sample application: Download

Sample applicaton features:
  1. Sample application provides simple and easy to use UI to play with application as shown below,

  2. It provides configurable on/off validations feature as shown below.
    //keep the flag true if csv contains header else not, this falg will help in validations.
    static isHeaderPresentFlag = true; 
    //keep the flag true if you want to validate each records length to match with header length, false otherwise.
    static validateHeaderAndRecordLengthFlag = true;
    //keep the flag true if you want file with only .csv extensions should be read, false otherwise.
    static valildateFileExtenstionFlag = true;

  3. Configurable token delimeter.
    static tokenDelimeter = ",";

Lets start with steps to start the sample application.
STEP 1:  
Download the sample application from here

STEP 2:  
Open command prompt, navigate to application folder and run "npm install" command.

(This step will download all development dependencies. It may take some time to dowload all dependencies. if you face any issue while downloading, open the command prompt as "Run as Administrator" and then execute "npm install")

Open command prompt, navigate to application folder and run "npm run server" command.
This step will start the server. 

STEP 4: 
After server starts, start the application by hitting "http://localhost:8080/" in browser.

STEP 5: 
Application will get started and you can play with application by importing sample csv files present along with bundle.

Screen will look like below,

Github page:
Download Sample application: Download

In this sample application, we are simply displaying the contents of uploaded csv file, 
If required you can also send the csv data to backend server.


<input type="file" 
 name="File Upload" 
 class="btn btn-primary" 

import { Component, OnInit, ViewChild } from "@angular/core";
import { Router }                       from "@angular/router";
import { FileUtil }                     from './file.util';
import { Constants }                    from './test.constants';

  template: require('./test.component.html')

export class TestComponent implements OnInit {

  fileImportInput: any;

  csvRecords = [];

  constructor(private _router: Router,
    private _fileUtil: FileUtil
  ) { }

  ngOnInit() { }

  fileChangeListener($event): void {

    var text = [];
    var files = $event.srcElement.files;

        alert("Please import valid .csv file.");

    var input = $;
    var reader = new FileReader();

    reader.onload = (data) => {
      let csvData = reader.result;
      let csvRecordsArray = csvData.split(/\r\n|\n/);

      var headerLength = -1;
        let headersRow = this._fileUtil.getHeaderArray(csvRecordsArray, Constants.tokenDelimeter);
        headerLength = headersRow.length; 
      this.csvRecords = this._fileUtil.getDataRecordsArrayFromCSVFile(csvRecordsArray, 
          headerLength, Constants.validateHeaderAndRecordLengthFlag, Constants.tokenDelimeter);
      if(this.csvRecords == null){
        //If control reached here it means csv file contains error, reset file.

    reader.onerror = function () {
      alert('Unable to read ' + input.files[0]);

    this.fileImportInput.nativeElement.value = "";
    this.csvRecords = [];


import { Injectable }       from '@angular/core';

export class FileUtil {

    constructor() {}

    isCSVFile(file) {

    getHeaderArray(csvRecordsArr, tokenDelimeter) {        
        let headers = csvRecordsArr[0].split(tokenDelimeter);
        let headerArray = [];
        for (let j = 0; j < headers.length; j++) {
        return headerArray;

    validateHeaders(origHeaders, fileHeaaders) {
        if (origHeaders.length != fileHeaaders.length) {
            return false;

        var fileHeaderMatchFlag = true;
        for (let j = 0; j < origHeaders.length; j++) {
            if (origHeaders[j] != fileHeaaders[j]) {
                fileHeaderMatchFlag = false;
        return fileHeaderMatchFlag;

    getDataRecordsArrayFromCSVFile(csvRecordsArray, headerLength, 
        validateHeaderAndRecordLengthFlag, tokenDelimeter) {
        var dataArr = []

        for (let i = 0; i < csvRecordsArray.length; i++) {
            let data = csvRecordsArray[i].split(tokenDelimeter);
            if(validateHeaderAndRecordLengthFlag && data.length != headerLength){
                    alert("Extra blank line is present at line number "+i+", please remove it.");
                    return null;
                    alert("Record at line number "+i+" contain "+data.length+" tokens, and is not matching with header length of :"+headerLength);
                    return null;

            let col = [];
            for (let j = 0; j < data.length; j++) {
        return dataArr;


import { Injectable } from '@angular/core';

export class Constants {
    static tokenDelimeter = ",";
    static isHeaderPresentFlag = true;
    static validateHeaderAndRecordLengthFlag = true;
    static valildateFileExtenstionFlag = true;

Please drop a comment if you see any error in sample application.

You may also like to see

Download binary file AngularJS + REST service

Configure Angular2 + Webpack + Maven Sample + Sample WebApp in Java

Number Range Spinner in Angular JS

Using JQuery Datatable with AngularJS 

What is Websocket. Use of Websocket. Client server example using Websockets, AngularJS and Java.

Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

Post a Comment