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
udayarangareddy mekalaudayarangareddy mekala 

ATTACHMENT FILE &IMAGE UPLOAD

CONTROLLER
-----------
public class EmployeeRelatedList {
 private Empolyee_Details__c empDetail;
 public blob picture{get;set;}
  public Integer counter {get; set;}
    public EmployeeRelatedList(ApexPages.StandardController stdController) {
        this.empDetail = (Empolyee_Details__c)stdController.getRecord();
    }
    public Pagereference save() {
        try{
             if(picture != null)
                          {
          Attachment attachment = new Attachment();
                   attachment.body = picture;
                    insert attachment;
                   }  
            upsert empDetail;
            return new Pagereference('/apex/Custom_pagenations');
        }
        catch(Exception e){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, e.getMessage()));
            return null;
        }
    }
}


VISUALFORCE PAGE
-----------------------
<apex:page standardController="Empolyee_Details__c" extensions="EmployeeRelatedList" tabStyle="Empolyee_Details__c">
  <apex:form >
    <apex:sectionHeader title="EmployeeInfo"/>
    <apex:pageBlock title="EmployeeData">
        <apex:pageBlockSection title="Empolyee basic Details" columns="2" collapsible="false">

              <apex:inputField value="{!Empolyee_Details__c.EmpId__c}"/>
            <apex:inputField value="{!Empolyee_Details__c.FirstName__c}"/>

            <apex:inputField value="{!Empolyee_Details__c.LastName__c}"/>
             <apex:inputField value="{!Empolyee_Details__c.MiddleName__c}"/>

            <apex:inputField value="{!Empolyee_Details__c.DateofBirth__c}"/>
             <apex:inputField value="{!Empolyee_Details__c.FatherMotherhusbandName__c}"/>

            <apex:inputField value="{!Empolyee_Details__c.MartialStatus__c}"/>
               </apex:pageBlockSection>
  
          <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="SAVE"/>
        </apex:pageBlockButtons>

        <apex:inputfile value="{!Empolyee_Details__c.id}"></apex:inputfile>

    </apex:pageBlock>
     </apex:form>

</apex:page>
    
           my requirement is i want to attahement the file as well as uplaod the image.i used the attachment object but i didint get any attatchment or  image upload.

THANKS &REGARDS
RANGA
 
Best Answer chosen by udayarangareddy mekala
Prabhat Kumar12Prabhat Kumar12
Try this

Apex Code 
 
public class EmployeeRelatedList {
 private Empolyee_Details__c empDetail;
 public blob picture{get;set;}
  public Integer counter {get; set;}
    public EmployeeRelatedList(ApexPages.StandardController stdController) {
        this.empDetail = (Empolyee_Details__c)stdController.getRecord();
    }
    public Pagereference save() {
        try{
             if(picture != null)
                          {
          Attachment attachment = new Attachment();
                   attachment.body = picture;
				   attachment.OwnerId = UserInfo.getUserId();
                   attachment.ParentId = 'Put record id here'; // the record the file is attached to
                    insert attachment;
                   }  
            upsert empDetail;
            return new Pagereference('/apex/Custom_pagenations');
        }
        catch(Exception e){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, e.getMessage()));
            return null;
        }
    }
}

VF Page
 
<apex:page standardController="Empolyee_Details__c" extensions="EmployeeRelatedList" tabStyle="Empolyee_Details__c">
  <apex:form >
    <apex:sectionHeader title="EmployeeInfo"/>
    <apex:pageBlock title="EmployeeData">
        <apex:pageBlockSection title="Empolyee basic Details" columns="2" collapsible="false">

              <apex:inputField value="{!Empolyee_Details__c.EmpId__c}"/>
            <apex:inputField value="{!Empolyee_Details__c.FirstName__c}"/>

            <apex:inputField value="{!Empolyee_Details__c.LastName__c}"/>
             <apex:inputField value="{!Empolyee_Details__c.MiddleName__c}"/>

            <apex:inputField value="{!Empolyee_Details__c.DateofBirth__c}"/>
             <apex:inputField value="{!Empolyee_Details__c.FatherMotherhusbandName__c}"/>

            <apex:inputField value="{!Empolyee_Details__c.MartialStatus__c}"/>
			
			<apex:outputLabel value="File" for="file"/>
			<apex:inputFile value="{!picture}" filename="yourfilename" id="file"/>
               </apex:pageBlockSection>
  
          <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="SAVE"/>
        </apex:pageBlockButtons>

        <apex:inputfile value="{!Empolyee_Details__c.id}"></apex:inputfile>

    </apex:pageBlock>
     </apex:form>

</apex:page>

 

All Answers

Prabhat Kumar12Prabhat Kumar12
Try this

Apex Code 
 
public class EmployeeRelatedList {
 private Empolyee_Details__c empDetail;
 public blob picture{get;set;}
  public Integer counter {get; set;}
    public EmployeeRelatedList(ApexPages.StandardController stdController) {
        this.empDetail = (Empolyee_Details__c)stdController.getRecord();
    }
    public Pagereference save() {
        try{
             if(picture != null)
                          {
          Attachment attachment = new Attachment();
                   attachment.body = picture;
				   attachment.OwnerId = UserInfo.getUserId();
                   attachment.ParentId = 'Put record id here'; // the record the file is attached to
                    insert attachment;
                   }  
            upsert empDetail;
            return new Pagereference('/apex/Custom_pagenations');
        }
        catch(Exception e){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, e.getMessage()));
            return null;
        }
    }
}

VF Page
 
<apex:page standardController="Empolyee_Details__c" extensions="EmployeeRelatedList" tabStyle="Empolyee_Details__c">
  <apex:form >
    <apex:sectionHeader title="EmployeeInfo"/>
    <apex:pageBlock title="EmployeeData">
        <apex:pageBlockSection title="Empolyee basic Details" columns="2" collapsible="false">

              <apex:inputField value="{!Empolyee_Details__c.EmpId__c}"/>
            <apex:inputField value="{!Empolyee_Details__c.FirstName__c}"/>

            <apex:inputField value="{!Empolyee_Details__c.LastName__c}"/>
             <apex:inputField value="{!Empolyee_Details__c.MiddleName__c}"/>

            <apex:inputField value="{!Empolyee_Details__c.DateofBirth__c}"/>
             <apex:inputField value="{!Empolyee_Details__c.FatherMotherhusbandName__c}"/>

            <apex:inputField value="{!Empolyee_Details__c.MartialStatus__c}"/>
			
			<apex:outputLabel value="File" for="file"/>
			<apex:inputFile value="{!picture}" filename="yourfilename" id="file"/>
               </apex:pageBlockSection>
  
          <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="SAVE"/>
        </apex:pageBlockButtons>

        <apex:inputfile value="{!Empolyee_Details__c.id}"></apex:inputfile>

    </apex:pageBlock>
     </apex:form>

</apex:page>

 
This was selected as the best answer
udayarangareddy mekalaudayarangareddy mekala
Thanks Kumar