public class MyDocuments { public document doc {get;set;} public List option {get;set;} public String len {get;set;} public List intTotal {get; set;} public MyDocuments(ApexPages.StandardController controller) { doc = (Document)controller.getRecord(); doc.folderId = UserInfo.getUserId(); option = new List(); option.add(new selectOption('','--None--')); intTotal = new List(); 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", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4LEqSAN", "datePublished": "2018-02-27T12:59:25.000Z", "author": { "@type": "Person", "name": "Sohan Raj Gupta", "url": "https://trailblazers.salesforce.com/profileView?u=0053A00000Cz62LQAR", "affiliation": { "@type": "Organization", "name": "Genpact" } } }, "suggestedAnswer": [] } } Skip to main content
Sandeep Yadav (Salesforce) a posé une question dans #Apex
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;

}

}

}

 
2 réponses
  1. 27 févr. 2018, 12:59
    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