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
mohimohi 

how to upload image in folder in document

Dear sir/mam

pls specify how to achieve  the functionality

thanks

Best Answer chosen by Admin (Salesforce Developers) 
mohimohi

this illustration is based on custom object :

public class Newuser
{
    ApexPages.StandardController controller;
    public Newuser(ApexPages.StandardController c)
    {
    this.controller = c;
    }


    public String ufname{get;set;}
    public String ulname{get;set;}
           // private String company;
    public String uphone{get;set;}
    public String uemail{get;set;}
    public String upassword{get;set;}
    public String url{get; set;}
           // private String leadStatus;
            //private String qp;
//public boolean duplicateemail{get; set;};
     
     /* public thecontroller() {
                this.qp = ApexPages.currentPage().getParameters().get('qp');
            } */

       /*     public String getFirstName() {
                  return this.firstName;
            }

            public void setFirstName(String firstName) {
                  this.firstName = firstName;
            }

            public String getLastName() {
                  return this.lastName;
            }

            public void setLastName(String lastName) {
                  this.lastName = lastName;
            }

            public String getCompany() {
                  return this.company;
            }

           public void setCompany(String company) {
                  this.company = company;
            }

            public String getEmail() {
                  return this.email;
            }

            public void setEmail(String email) {
                  this.email = email;
            }
            public String getStatus() {
                  return this.leadStatus;
            }

            public void setStatus() {
                  this.leadStatus = 'Open-Not Contacted';
            }
          public String getPhone() {
                  return this.Phone;
            }

            public void setPhone(String Phone) {
                  this.Phone = Phone;
            }
            public String getPassword() {
                  return this.Password;
            }

            public void setPassword(String Password) {
                  this.Phone = Password;
            }  */
          
           
            public PageReference save()
            {
               
               try
               {     
                     Document d = (Document) controller.getRecord();
                     Folder folder = [Select Id  From Folder Where Name= :'CandidateImages'];
                     d.folderid=folder.id;
                     insert d;
                     this.url='/servlet/servlet.FileDownload?file='+d.id;
                     List<Candidateinfo__c> myLead=[select LastName__c , Email__c from Candidateinfo__c where Email__c =:uemail];
                     if(myLead.size() == 0)
                     {
                            System.debug('sss1');
                          //System.debug('>>>>>' +this.ulname);
                            Candidateinfo__c newcandidate = new CandidateInfo__c(LastName__c=this.ulname,
                            FirstName__c=this.ufname,
                            Phone__c=this.uphone,
                            Email__c=this.uemail,
                            password__c=this.upassword,
                            imageurl__c=this.url);
                      // string key='id';
                        //string value=this.firstName;
                       //ApexPages.currentPage().setParameters().put(key, value);
                            insert newcandidate;
                        
                        
                        return Page.success;
                    
                    }
                   else
                     {
                      ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error, 'my error msg');
                      ApexPages.addmessage(myMsg);
                      return Page.Failure;
                     }
                 }
                 catch (Exception e)
                    {
                      ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error, e.getmessage());
                      ApexPages.addmessage(myMsg);
                    }
                                   
                 return null;
            }
            
 }

visual force

<apex:page standardController="document"  extensions="Newuser" id="mypage">
 <apex:form id="registration">
      <apex:pageBlock id="pageblock1" >
         <h1>Inserting value for  candidate</h1>
         <p>This is a test page for adding Candidate.</p>
         <table width="100%" bgcolor="E6CFD4">
         <tr><td width="20%">
         <b>First name: </b></td>
         <td width="80%"><apex:inputText value="{!ufname}" id="fname" ></apex:inputText></td>
         </tr>
         <tr>
         <td width="20%"><b>Last name:</b></td>
         <td width="80%"><apex:inputText value="{!ulname}" id="lname"></apex:inputText></td>
          </tr>
          <tr>
          <td width="20%"><b>Phone:</b></td>
          <td width="80%"><apex:inputText value="{!uphone}" id="uphone"></apex:inputText></td>
          </tr>
          <tr>
          <td width="20%"><b>Email address:</b></td>
          <td width="80%">
          <apex:inputText value="{!uemail}" id="mail"></apex:inputText></td>
          </tr>
          <tr>
          <td width="20%"><b>Password:</b></td>
          <td width="80%"><apex:inputSecret value="{!upassword}" id="password"></apex:inputSecret></td>
          </tr>
         <tr>
          <td width="20%"><b>Upload Image:</b></td>
          <td width="80%"> <apex:inputFile value="{!document.body}" filename="{!document.name}"/>
          </td>
          </tr>
         </table>
      
      <apex:commandButton action="{!save}" onclick="return cvalidation();" value="save candidate info"/>
      </apex:pageBlock>
     <script type="text/javascript">
        function cvalidation()
        {
        var txtfirstname=document.getElementById("{!$Component.mypage.registration.pageblock1.fname}").value;
        var txtlname=document.getElementById("{!$Component.mypage.registration.pageblock1.lname}").value;
        var txtCompany=document.getElementById("{!$Component.mypage.registration.pageblock1.uphone}").value;
        var txtmail=document.getElementById("{!$Component.mypage.registration.pageblock1.mail}").value;
       
           
        
        if(txtfirstname=="")
        {
        alert('plz enter the first name');
         return false;
        }
        if(txtlname=="")
        {
        alert('plz enter the last name');
        
        return false;
        }
        if(txtCompany=="")
        {
        alert('plz enter the Company');
        return false;
        }
        if(txtmail=="")
        {
        alert('plz enter emaiid');
        return false;
        }
        return true;
        }
        </script>
      </apex:form>
   </apex:page>