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
Carlos NaranjoCarlos Naranjo 

Page behavior after inserting image

Hello all,

I was hoping that someone here could help me with an issue that it is driving me crazy.

I have a visualforce page that will allow users to insert to picture and view those pictures right after insertion. At the sametime those picture will be save on the Note & Attachment section within the object.

All this is done on a custom object with and extension to use the attachment object.

The wired thing is that if I save the first picture (pictureOne on the code) I received the page message telling me that it is been inserted successfully and the picture. That is all I see, it will remove the "Browse" from the second picture and even the one for the first. This is annoying because then the user needs to refresh the page to get the options to insert a second picture or to select another picture for the pictureOne.


-------------------------------------

public with sharing class AttachmentController {
   
   String recId;
   public boolean con { get; set; }
   public boolean con2 { get; set; }
   
   public AttachmentController(ApexPages.StandardController controller) {
   recId = controller.getId(); 
         

    }
      
   public Attachment pictureOne {
       get {
          if (pictureOne == null)
          pictureOne = new Attachment();
          return pictureOne;
           }
      set;
  }
 
   
  public PageReference savePictureOne() {

    pictureOne.OwnerId = UserInfo.getUserId();
    pictureOne.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureOne.Name = 'W1' + pictureOne.name;
   

    try {
     
      insert pictureOne;
      con = true;
     
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      pictureOne = new Attachment();
      
      
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
   
    }   
   
    public String getFileId() {
        String fileId = '';
        List<Attachment> attachedFiles = [select Id from Attachment where Name LIKE 'W1%' AND parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId = attachedFiles[0].Id;
        }
        return fileId;   
    }
   
   
   
   
     public Attachment pictureTwo {
       get {
          if (pictureTwo == null)
          pictureTwo = new Attachment();
          return pictureTwo;
           }
      set;
    }
    public PageReference savePictureTwo() {

    pictureTwo.OwnerId = UserInfo.getUserId();
    pictureTwo.ParentId = ApexPages.currentPage().getParameters().get('id');
    pictureTwo.Name = 'W2' + pictureTwo.name;
    

    try {
     
      insert pictureTwo;
      con2 = true;
     
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      pictureTwo = new Attachment();
      
      
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
   
    }   
   
    public String getFileId2() {
        String fileId2 = '';
        List<Attachment> attachedFiles = [select Id from Attachment where Name LIKE 'W2%' AND parentId =:recId order By LastModifiedDate DESC limit 1];
        if( attachedFiles != null && attachedFiles.size() > 0 ) {
            fileId2 = attachedFiles[0].Id;
        }
        return fileId2;   
    }
}


Visualforce page:

<apex:page StandardController="Visit_Report__c"  extensions="AttachmentController" showHeader="false">
     <apex:form id="theForm">
       <apex:pageBlock > <apex:pageMessages />
           <table>
             <tr>
              <td>
              <apex:outPutPanel id="imagePanel" rendered="{!FileId!=null}">
                 <img id="theImage"  src="/servlet/servlet.FileDownload?file={!FileId}" style="vertical-align:left" width="250" height="300" onclick="window.open('/servlet/servlet.FileDownload?file={!FileId}');"/>
              </apex:outPutPanel>
                
              <apex:outPutPanel id="thePanel" rendered="{!con == false}">
                 <apex:pageBlockSection >
                 <apex:inputFile value="{!pictureOne.body}" filename="{!pictureOne.name}"/>
                 <apex:commandButton value="Save" action="{!savepictureOne}" /><br />
                 </apex:pageBlockSection>
              </apex:outPutPanel>
              
              </td>
            <td>
              <apex:outPutPanel id="imagePanel2" rendered="{!FileId2!=null}">
                      <img id="theImage2"  src="/servlet/servlet.FileDownload?file={!FileId2}" style="vertical-align:left" width="250" height="300" onclick="window.open('/servlet/servlet.FileDownload?file={!FileId2}');"/>
                 </apex:outPutPanel>
                
               <apex:outPutPanel id="thePanel2" rendered="{!con == false}">
                   <apex:pageBlockSection >
                   <apex:inputFile value="{!pictureTwo.body}" filename="{!pictureTwo.name}"/>
                   <apex:commandButton value="Save" action="{!savepictureTwo}" /><br />
                   </apex:pageBlockSection>
               </apex:outPutPanel>
           
                </td>
             </tr>
           </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Anyone knwos why this is happening?
bob_buzzardbob_buzzard
(Cross posting my answer from stack exchange for the benefit of those that arrive at this question)

In your second output panel you are using con rather than con2 to decide whether to render. As con is set to true when picture 1 is uploaded, this panel won't be displayed:

<apex:outPutPanel id="thePanel2" rendered="{!con == false}">
  <apex:pageBlockSection >
  <apex:inputFile value="{!pictureTwo.body}" filename="{!pictureTwo.name}"/>
  <apex:commandButton value="Save" action="{!savepictureTwo}" />
  </apex:pageBlockSection>
</apex:outPutPanel>
If you change the first line to:

<apex:outPutPanel id="thePanel2" rendered="{!con2 == false}">

then the panel will display until picture 2 is uploaded.