When am trying to click on Attach Another file button ,selected file on first input file is getting null.

Here is the code.

    public class SamplePageController{

 public  List<Attachment> allFileList {get; set;}
 public boolean showMorePanel {get;set;}

 public SamplePageController(){
  allFileList2 = new list<Attachment>();

 public void addMoreFiles(){
     allFileList.add(new Attachment());
     showMorePanel = true;   


 <apex:page controller="SamplePageController">
 <apex:form >
  <apex:pageBlock >
   <apex:pageBlockSection title="Attach Files" columns="1">
   <apex:outputPanel id="morefiles"> 
     <apex:outputPanel rendered="{!showMorePanel}">
     <apex:repeat value="{!allFileList}" var="f">
       <apex:inputText label="File Name"/>
       <apex:inputfile value="{!f.body}" filename="{!f.name}">
  <apex:outputPanel id="Mypanel">   
    <apex:outputPanel >   
    <apex:actionregion >    
    <apex:commandButton action="{!addMoreFiles}" value="Attach Another File" reRender="morefiles" style="float: left;" />

enter image description here

When I click on Attach Another file button,value of first input file is getting null.

enter image description here


I know this is not an exact answer to your question but I think that, in consideration for heap size, you may want to change your code so the "Attach New File" button actually uploads the first attachment and then re-initializes the variable so a new file can be uploaded, instead of allowing X files to be uploaded at the same time.

That way you avoid the inevitable problems that will arise (after you fix the code) if the user tries to attach X number of files, and then the sheer size of those attachments blows your heap.

I've implemented this solution before, just as I described it, and it works wonders.

  • Also. The OOB SFDC attachment UI doesn't permit multiple attachments to be uploaded in the same POST and were there a solution for the OP design, one would have thought SFDC would have provided it by now as the user benefit is obvious. – cropredy Feb 23 '16 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.