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
LB_DDLB_DD 

Upload CSV file from C# to Visualforce page, is possible?

Hello all,

 

I try to Upload a CSV file from a C# Winform application to a Visualforce page. I try to POST on the VF page, but can't get the file!!!

 

My VF Page:

 

<apex:page sidebar="false" showHeader="false" controller="CSVUploadCallbackCtr" action="{!init}">
      <apex:form enctype="multipart/form-data">
          <apex:sectionHeader title="Upload data from CSV file"/>
          <apex:pagemessages />
          <apex:pageBlock title="Select best option!">
            <center>
              <h2>Conteudo:</h2><br />
              <apex:inputTextarea rows="1" id="gli"/><br /><br /><h2>ou</h2><br />              
              <apex:inputFile value="{!contentFile}"  id="file" /> 
              <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/>              
            </center>               
          </apex:pageBlock>
      </apex:form>
</apex:page>

 

The Controller:

public with sharing class FarmaciaCSVUploadCallbackCtr {
   public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
 
    /* Try read here, but only work for the Input TextArea, not for the CSV file */
    public Pagereference init()
    {   
        String Gli = ApexPages.currentPage().getParameters().get('gli');   
        String[] lines = Gli.split('\n');
        System.Debug('=======> init() / Gli='+Gli);
        try{
            nameFile=contentFile.toString();
        }catch(Exception ex){}

System.Debug('=======> init() / nameFile='+nameFile+' / Gli='+Gli);                 

       for (Integer i=1;i<lines.size();i++)
       {
        System.Debug('=======> init() / line='+lines[i]); 
       }
       return null;
    }

    /* FROM C# this is not executed, only when submit from Visualforce Page. */    
    public Pagereference ReadFile()
    {   
        try{
            nameFile=contentFile.toString();
            filelines = nameFile.split('\n');
        }catch(Exception ex){}
System.Debug('=======> ReadFile() / nameFile='+nameFile+' / filelines='+filelines);        
        for (Integer i=1;i<filelines.size();i++)
        {
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
System.Debug('=======> ReadFile() / filelines='+filelines[i]); 
        }
        return null;
    }
    

}

 

On the Visualforce page, when POST from my Win App, the field "Gli" (a inputTextArea) receive the text value on the controller, but the CSV file, don't !

 

Anyone have a solution or another idea to POST csv files to a Visualforce Page?

 

Best Regards,

LB