+ Start a Discussion
vishal chaudhary 16vishal chaudhary 16 

how to send pdf file from apex form

hi there, i am trying post pdf file from visual page to my controller using apex:form and send it third party using rest api, but on controller end i am not getting my pdf file.its field is null,
visual page
 
<apex:page controller="SendAgreementExt" docType="html-5.0" >

<apex:form enctype="multipart/form-data">
<apex:pageBlock >
       <apex:pageBlockButtons >
        <apex:commandButton value="Send" action="{!send}"/>
    </apex:pageBlockButtons>

        <apex:pageBlockSection showHeader="false" columns="2">
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="File Name" for="fileName" />
                <input type="text" name="name"/>
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem >
                <apex:outputLabel value="File" for="file" />
                <input type="file" name="attachment"/>
            </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
            </apex:pageBlock>
            
    </apex:form>
{!LeadMessage}
</apex:page>
controller
Public with Sharing Class SendAgreementExt
{

       public String LeadMessage{get;set;}
       public String name{get;set;}
       public String agreement{get;set;}
      public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
    }
  set;
  }
        public void send()
        {
         system.debug('========name======='+name); 
        system.debug('========attachment======='+attachment);   
           JSONParser parser;
             Http h = new Http();
         ID leadid=ApexPages.currentPage().getParameters().get('id');       
         string redirect_uri =URL.getSalesforceBaseUrl()+'/apex/sendagreementvf';
         
            HttpRequest req = new HttpRequest();
               req.setMethod('POST');
               req.setHeader('Content-Length', '512'); 
               req.setHeader('Access-Token', Label.echosign_access_token); 
               req.setEndpoint('https://api.na1.echosign.com/api/rest/v5/transientDocuments');
              req.setBody('File-Name='+attachment.name+'&Mime-Type=pdf&File='+attachment.body);
               req.setHeader('Content-Type', 'application/x-www-form-urlencoded');  
               HttpResponse res;
               
               res = h.send(req);
               system.debug('==='+res.getBody());
               LeadMessage=res.getBody();
            parser = JSON.createParser(res.getBody());
                    
        }


}

Please Help
 
Marcel dos SantosMarcel dos Santos

You have to use apex:inputFile instead of the plain html input type="file" tag:

<apex:inputFile value="{!attch.Body}" filename="{!attch.Name}" id="fileattch"/>


In your controller you will have:

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