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

Upload CSV File

Upload .csv file in the vf page
sandy sfdcsandy sfdc
Hi Siva

Try this,
<apex:page controller="Upload_CSV_File_Controller">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="4"> 
                  <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                  <apex:commandButton value="Upload" action="{!UploadCsvFile}"/>
        <apex:pageBlock >
           <apex:pageblocktable value="{!LeadList}" var="ld">
              <apex:column value="{!ld.FirstName}" />
              <apex:column value="{!ld.LastName}" />
              <apex:column value="{!ld.Company}" />
              <apex:column value="{!ld.Phone}" />


public with sharing class Upload_CSV_File_Controller {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<Lead> LeadList{get;set;}
  public Upload_CSV_File_Controller(){
    csvFileLines = new String[]{};
    LeadList = New List<Lead>(); 
  public void UploadCsvFile(){
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            for(Integer i=1;i<csvFileLines.size();i++){
               Lead LeadObj = new Lead() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               LeadObj.FirstName = csvRecordData[0] ;             
               LeadObj.LastName = csvRecordData[1];
               LeadObj.Company = csvRecordData[2];
               LeadObj.Phone = csvRecordData[3];   
        insert Leadlist;
        catch (Exception e)
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');


Hi Sandy,

It's very nice.................but actually i  have been trying the upload the multiple files using single upload button in UI...........