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
Jay reddyJay reddy 

attach button in VF page

Hello,

Can anyone help how to add Upload / attach button in VF page. On clicking it it should allow uploading documents from local computer.

thanks,
Girish
Arvind KumarArvind Kumar
Hi Reddy,

Use this vf page & controller for upload attachment.

VF Page:
<apex:page controller="AttachmentUploadController">  
  <apex:sectionHeader title="Visualforce Example" subtitle="Attachment Upload Example"/>

  <apex:form enctype="multipart/form-data">
    <apex:pageMessages />
    <apex:pageBlock title="Upload a 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:form>
</apex:page>

Controller:
public with sharing class AttachmentUploadController {

  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }

  public PageReference upload() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = '0037000000lFxcw'; // the record the file is attached to
    attachment.IsPrivate = true;

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

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

}

If you have any query please let me know.

if it solves your problem mark as a best anwer.

Thanks,
Arvind kumar 
Jay reddyJay reddy
Thank you @Arvind for your help. But is it possible to save the uploaded attachment to the NOTES and Attachments at Opp level
Arvind KumarArvind Kumar
Hi Reddy,

Are you using Vf page for Opportunity? If you are using Vf page for opportunity then you can copy below code & paste in your org.
If you are using for another object then change object name in Vf page (from here: standardController="Opportunity"). After that you can use it according your requirement which object you want to do.

Folllow the below steps:
1.) Copy Vf page & Class Extensions and paste in your org.
2.) Use for your require object & Put VF page on the pageLayout.
3.) It will show on detail page.
4.) Upload your attachment in Vf page section.
5.) It will show in Note & Attachment related list.

Vf Page:
<apex:page standardController="Opportunity" extensions="AttachmentUploadController" >  
  <apex:sectionHeader title="Visualforce Example" subtitle="Attachment Upload Example"/>

  <apex:form enctype="multipart/form-data">
    <apex:pageMessages />
    <apex:pageBlock title="Upload a 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:form>
</apex:page>

Class Extension:
public with sharing class AttachmentUploadController {

   public string opp;

     public AttachmentUploadController(ApexPages.StandardController controller) {
            opp = controller.getRecord().id;
 
     }

     public Attachment attachment {
       get {
           if (attachment == null)
               attachment = new Attachment();
               return attachment;
           }
      set;
      }
  
 
     public PageReference upload() {

      attachment.OwnerId = UserInfo.getUserId();
      attachment.ParentId = opp; // the record the file is attached to
      attachment.IsPrivate = true;

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

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

}


Please use it. It will work for you.

If you have any issue please let me know.

Thanks,
Arvind Kumar