function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
edukondalu thodetiedukondalu thodeti 

how to get first row header names of the CSV files

Hi
how to get the first row header names of the csv files when we are uploading the file using lightning component
i need to that header names in the box of next page when i click on the next page lightning button 
Pls help me
AbhishekAbhishek (Salesforce Developers) 
In JS, using FileReader, you can read the file and send the contents to the apex as a String.


Like this - 

this.fileReader= new FileReader();
    console.log(this.fileReader);
    this.fileReader.onloadend = (() => {
        this.fileContents = this.fileReader.result;
        console.log(this.fileContents);

Then in apex, using the below logic you will get the CSV headers - 

String stringFile   = EncodingUtil.urlDecode(fileContents, 'UTF-8');
String fileLines     = stringFile.split('\n')[0];
List<String> csvHeaders   = new List<String>();

for(String strline : fileLines.split(',')){
    csvHeaders.add(strline.trim());
}



"fileContents" is a String variable that you will pass from JS to the apex.


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.