Friday, 11 April 2014

Uploading an IMAGE Using VF and Apex Code....

Visual force Code :

<apex:page controller="image">
 <apex:form >
  <apex:pageBlock >
   <apex:pageBlockSection title="Images" columns="1" >
    <apex:pageBlockSectionItem >
     <apex:outputLabel >Pictures</apex:outputLabel>
      <apex:outputPanel >
        <apex:image url="https://c.ap1.content.force.com/servlet/servlet.FileDownload?file={!document.id}"
              height="100" width="100" rendered="{!showimage}"/>
        <apex:inputFile value="{!document.body}" fileName="{!document.name}" ></apex:inputFile>
        <apex:commandButton value="Upload Image" action="{!upload_img}" />  
          <!--reRender="imgref" />-->
        <apex:messages />
      </apex:outputPanel>
    </apex:pageBlockSectionItem>
   </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Apex Programming Code :

public with sharing class image {
    public Document document {
        get {
            if (document == null)
                document = new Document();
                 return document;
        }
        set;        //{ document.type = 'jpg'; }
    }
    public String showimage { get; set; }
    public PageReference upload_img() {
        document.AuthorId = UserInfo.getUserId();
        document.FolderId = UserInfo.getUserId();
        try{
            insert document;          
        }catch(DMLException e){
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading
              file'));
            return null;
        }
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Image uploaded
                   successfully'));
        return null;
    }
}

No comments:

Post a Comment