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
Nirmal9114Nirmal9114 

Need to Display the Attachment

Please Tell me..where is my mistake I not able to attach a file and display it.

VF PAGE :

<apex:page controller="AttachmentUploadController" sidebar="False">  
  <apex:sectionHeader title="Notes And Attachment" subtitle="Attach a File"/>

  <apex:form >
    <apex:pageMessages />
    <apex:pageBlock title="Upload an Attachment">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!upload}" value="Save"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!attachment.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

       <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Description" for="description"/>
          <apex:inputTextarea value="{!attachment.description}" id="description"/>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>

    </apex:pageBlock>
    
    <apex:pageBlock >
    <apex:pageBlockTable value="{!lstAttach}" var="att" >
    
        <apex:column HeaderValue="Name" >
                 <apex:outputField value="{!att.name}" />
        </apex:column>
        
        <apex:column HeaderValue="Description" >
                 <apex:outputField value="{!att.Description}" />
        </apex:column>
        
        <apex:column HeaderValue="Last Modified Date" >
                <apex:outputField value="{!att.LastModifiedDate }" />
       </apex:column>

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

CONTROLLER :

public class AttachmentUploadController {

    public String rightObject { get; set; }

    public String listatt { get; set; }

    public String item { get; set; }
    public String AccountTable { get; set; }
    String parentId;
 
    public list<Attachment> lstAttach{ get; set;}
      public Attachment attachment{ get; set;}
      
    public String getAttachmentUploadController() {
        return null;
    }
 
    public AttachmentUploadController() {
    attachment = new Attachment();
    
        User UserId = [Select Id, contactid, contact.accountId from user where id =: UserInfo.getUserId() limit 1];
        Id AccId=UserId.contact.accountId;
        lstAttach = [Select name,description,LastModifiedDate from attachment where OwnerId =: UserInfo.getUserId() and ParentId = : AccId];
    
      }
      
 
    public PageReference upload() {
   Attachment attachment = new Attachment();
   attachment.OwnerId = UserInfo.getUserId();
   attachment.ParentId = parentId; // Account- George P. Artner record id
   attachment.IsPrivate = true;
     try {
      insert attachment;
     
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }

}
Naval Sharma4Naval Sharma4
Hi Nirmal,

Your code is correct but this needs some minor changes.
VF Page
<apex:page controller="AttachmentUploadController" sidebar="False">  
  <apex:sectionHeader title="Notes And Attachment" subtitle="Attach a File"/>

  <apex:form >
    <apex:pageMessages />
    <apex:pageBlock title="Upload an Attachment">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!upload}" value="Save" rerender="attList"/>
      </apex:pageBlockButtons>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!attachment.name}" id="fileName"/>
        </apex:pageBlockSectionItem>

       <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Description" for="description"/>
          <apex:inputTextarea value="{!attachment.description}" id="description"/>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>

    </apex:pageBlock>
    
    <apex:pageBlock  id="attList">
    <apex:pageBlockTable value="{!lstAttach}" var="att" >
    
        <apex:column HeaderValue="Name" >
                 <apex:outputField value="{!att.name}" />
        </apex:column>
        
        <apex:column HeaderValue="Description" >
                 <apex:outputField value="{!att.Description}" />
        </apex:column>
        
        <apex:column HeaderValue="Last Modified Date" >
                <apex:outputField value="{!att.LastModifiedDate }" />
       </apex:column>

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

Apex Controller code
public class AttachmentUploadController {

    public String rightObject { get; set; }

    public String listatt { get; set; }

    public String item { get; set; }
    public String AccountTable { get; set; }
    String parentId;
 
    public list<Attachment> lstAttach{ get; set;}
      public Attachment attachment{ get; set;}
      
    public String getAttachmentUploadController() {
        return null;
    }
 
    public AttachmentUploadController() {
    attachment = new Attachment();
    
        User UserId = [Select Id, contactid, contact.accountId from user where id =: UserInfo.getUserId() limit 1];
        Id AccId=UserId.contact.accountId;
        lstAttach = [Select name,description,LastModifiedDate from attachment where OwnerId =: UserInfo.getUserId() and ParentId = : AccId];
    
      }
      
 
    public PageReference upload() {
   Attachment attachment = new Attachment();
   attachment.OwnerId = UserInfo.getUserId();
   attachment.ParentId = parentId; // Account- George P. Artner record id
   attachment.IsPrivate = true;
     try {
      insert attachment;
      attachment.body = null;
      lstAttach = [Select name,description,LastModifiedDate from attachment where OwnerId =: UserInfo.getUserId() and ParentId = : AccId];
     
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      attachment.body = null;
      
      return null;
    }

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
    return null;
  }

}

 
Nirmal9114Nirmal9114
Hi Naval,

after your code i am getting this error.

Visualforce Error

apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute.


Please help.