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
Sandeep YadavSandeep Yadav 

Upload files depend on picklist value

Hello everyone,
I want to upload a number of files when I select a value in the picklist.i.e. when I select 2 in picklist then it uploads only two files and so on.
Here is the code what I did to achieve this.
Thanks in advance
VF Page--

<apex:page standardController="Document" extensions="MyDocuments">
  <apex:form >
      <apex:pageBlock >          
          <apex:selectList value="{!len}" size="1">
              <apex:selectOptions value="{!option}"/>
              <apex:actionSupport event="onchange" action="{!uploadMultiFile}" reRender="lab"/>
          </apex:selectList>
      </apex:pageBlock>
      
      <apex:pageBlock title="UpLoad A File">
          <apex:pageBlockSection id="lab">
              <apex:repeat value="{!total}" var="t">
                  <apex:inputFile value="{!doc.body}" fileName="{!doc.name}"/>
              </apex:repeat>
              <apex:commandButton value="Save" action="{!save}"/>

          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

---------------------------------------------------------------------------------------------------------------------
Controller--

public class MyDocuments {
    
    public document doc {get;set;}
    public List<selectOption> option {get;set;}
    public String len {get;set;}
    public Integer total {get;set;}
    
    
    public MyDocuments(ApexPages.StandardController controller) 
    {
        doc = (Document)controller.getRecord();
        doc.folderId = UserInfo.getUserId();
        
        option = new List<selectOption>();
        option.add(new selectOption('','--None--'));
        for(Integer i=1; i<=10; i++)
        {
            option.add(new selectOption('val'+i,string.valueOf(i)));
        }
        
    }
    
    public void uploadMultiFile()
    {
        if(len == 'val1')
        {
            total = 1;
        }
        if(len == 'val2')
        {
            total = 2;
        }
    }

}

 
Best Answer chosen by Sandeep Yadav
Sohan Raj GuptaSohan Raj Gupta
Hi Sandeep,

You can not rerender inputFile using actionsupport, so you have to use javascript for this.

Check below code for your reference and change this as per your requirment.
 
<apex:page standardController="Document" extensions="MyDocuments">
  <apex:form >
      <apex:pageBlock >          
          <apex:selectList value="{!len}" size="1" onchange="showFiles();" styleClass="fileCount">
              <apex:selectOptions value="{!option}"/>              
          </apex:selectList>
      </apex:pageBlock>
      
      <apex:pageBlock title="UpLoad A File">
          <apex:pageBlockSection id="lab">
              <apex:repeat value="{!intTotal}" var="t">
                  <apex:inputFile value="{!doc.body}" fileName="{!doc.name}" style="display:none" styleclass="file_{!t} files"/>
              </apex:repeat>
              <br />
              <apex:commandButton value="Save" action="{!save}"/>

          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script type="text/javascript">
  
      function showFiles(){
          var fileCount = jQuery(".fileCount").val();
          //alert(fileCount )
          jQuery(".files").hide();
          jQuery(".files").val('');
          
          for(i=1; i<=fileCount;i++){
              jQuery(".file_"+i).show();
          }
      }
      
      
  </script>
</apex:page>
 
public class MyDocuments {
    
    public document doc {get;set;}
    public List<selectOption> option {get;set;}
    public String len {get;set;}
    public List<Integer> intTotal {get; set;}
     
    public MyDocuments(ApexPages.StandardController controller)
    {
        doc = (Document)controller.getRecord();
        doc.folderId = UserInfo.getUserId();

        option = new List<selectOption>();
        option.add(new selectOption('','--None--'));
        
        intTotal = new List<Integer>();
        for(Integer i=1; i<=10; i++)
        {
            option.add(new selectOption(string.valueOf(i),string.valueOf(i)));
            intTotal.add(i);
        }
    }
}

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta

All Answers

pradeep kumar yadavpradeep kumar yadav
You have to call javascript on file upload to check file count and show alert as per your requirement.
<apex:inputFile value="{!doc.body}" fileName="{!doc.name}"  onchange="TestFileType(this.id,this.value);"/>

<script type="text/javascript">
    function TestFileType(elemid,fileName) {
        if (!fileName) return;
        else {
               var len = '{!len}';
               var abc = fileName.split(",");
               if(len != abc.length)
                 alert('You can only select '+ len + 'files');
         }
    }
  </script>

 
Sohan Raj GuptaSohan Raj Gupta
Hi Sandeep,

You can not rerender inputFile using actionsupport, so you have to use javascript for this.

Check below code for your reference and change this as per your requirment.
 
<apex:page standardController="Document" extensions="MyDocuments">
  <apex:form >
      <apex:pageBlock >          
          <apex:selectList value="{!len}" size="1" onchange="showFiles();" styleClass="fileCount">
              <apex:selectOptions value="{!option}"/>              
          </apex:selectList>
      </apex:pageBlock>
      
      <apex:pageBlock title="UpLoad A File">
          <apex:pageBlockSection id="lab">
              <apex:repeat value="{!intTotal}" var="t">
                  <apex:inputFile value="{!doc.body}" fileName="{!doc.name}" style="display:none" styleclass="file_{!t} files"/>
              </apex:repeat>
              <br />
              <apex:commandButton value="Save" action="{!save}"/>

          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script type="text/javascript">
  
      function showFiles(){
          var fileCount = jQuery(".fileCount").val();
          //alert(fileCount )
          jQuery(".files").hide();
          jQuery(".files").val('');
          
          for(i=1; i<=fileCount;i++){
              jQuery(".file_"+i).show();
          }
      }
      
      
  </script>
</apex:page>
 
public class MyDocuments {
    
    public document doc {get;set;}
    public List<selectOption> option {get;set;}
    public String len {get;set;}
    public List<Integer> intTotal {get; set;}
     
    public MyDocuments(ApexPages.StandardController controller)
    {
        doc = (Document)controller.getRecord();
        doc.folderId = UserInfo.getUserId();

        option = new List<selectOption>();
        option.add(new selectOption('','--None--'));
        
        intTotal = new List<Integer>();
        for(Integer i=1; i<=10; i++)
        {
            option.add(new selectOption(string.valueOf(i),string.valueOf(i)));
            intTotal.add(i);
        }
    }
}

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta
This was selected as the best answer