Here is my solution:
1: FE: Angular 10, NPM install ngx-papaparse
Then import in the component
import { Papa } from "ngx-papaparse";
2: one input with type file only accept csv file on FE
<input type="file" id="csv-file" accept=".csv" (change)="onFileSelected($event)">
3: one button to read CSV file
4: we get the file data from the onFileSelected function
const files = event.target.files;
if (files.length === 0)
return;
this.selectedFile = files[0] as File;
5: call read CSV file function
this.papa.parse(this.selectedFile, {
delimiter: ",",
newline: "",
header: true,
dynamicTyping: true,
complete: (result) => {
this.fileData= result.data;
}
});
6: you should get the object fileData
Tips: check the papaparse documentation to see what configuration you need.
https://www.papaparse.com/
eg: header: true --> means you will get the headers in the result object
'challanges.csv'
to a file that you get from a file input or some drag and drop – Asdic