Read CSV File

107 views
Skip to first unread message

Arindam saha

unread,
Jan 16, 2019, 8:56:39 AM1/16/19
to Angular and AngularJS discussion
I am creating a static application with Angular 6. My requirement is to read data from a csv file that resides in asset folder. Can anyone please help me on that?

Sander Elias

unread,
Jan 17, 2019, 11:26:48 PM1/17/19
to Angular and AngularJS discussion
Hi Arindam,

Something like this:

csvData = [];
readData() {
this.http
.get<string>('/assets/data.csv')
.pipe(
map((csv: string) => csv.split('/n').map(line => line.split(','))),
tap(array => (this.csvData = array))
)
.subscribe();
}

In your service would do.
Regards
Sander

Arindam saha

unread,
Jan 19, 2019, 1:27:06 PM1/19/19
to Angular and AngularJS discussion
My CSV format is like below,

Course_Name,Offering_Id,Start_Date,End_Date,SPOC_ID

Learning 1,112323,18/01/2019,20/01/2019,676776
Learning 2,111232,19/01/2019,22/01/2019,898898
Learning 3,121232,18/01/2019,30/01/2019,989898
Learning 4,321232,18/01/2019,25/01/2019,767676


The code suggested by you as below,
readData() {
this.jdwHttp.get<string>('/assets/weekly_learning.csv').pipe(
map((csv: string) => csv.split('/n').map(line => line.split(','))),
tap(array => (this.csvData = array))
).subscribe();

And I am getting the below error while running,

Capture.PNG

Tito

unread,
Jan 19, 2019, 8:33:41 PM1/19/19
to Angular and AngularJS discussion
Your second split(.) is
Not looking for a comma
As Sanders showed you use a comma

Sander Elias

unread,
Jan 20, 2019, 1:28:43 AM1/20/19
to Angular and AngularJS discussion
Looks like your server isn't setting the correct content-type.  Make sure that is corrected on the server. If you can't, use the http-options to fix it client side.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages