2

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" />
                 <apex:inputText value="{!attachment.name}"></apex:inputText>
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem >
                <apex:outputLabel value="File" for="file" />
                 <apex:inputFile value="{!attachment.body}"></apex:inputFile>
            </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());

        }
}
  • 2
    The apex:inputFile tag is the simplest way to bind a file input to controller properties including a Blob property to hold the data. – Keith C May 18 '16 at 20:48
  • i tried this also <apex:inputFile value="{!attachment.body}"></apex:inputFile> but how can i get file from this tag on controller – vishal chaudhary May 18 '16 at 20:58
  • in debug it shows Attachment:{Body=Blob[46003], Name=test} how can i extract pdf file from it or send pdf file through my api – vishal chaudhary May 18 '16 at 21:01
  • You need to research what formats the API allows. You can base 64 encode the Blob using EncodingUtil.base64Encode, but that only helps if they support that format. – Keith C May 18 '16 at 21:19
  • will it be work like this String pdf=base64Encode(attachment.body); – vishal chaudhary May 18 '16 at 21:22
0

thanx guys for your help i have done it ajax

<apex:page controller="SendAgreementExt">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <form id="data" method="post" enctype="multipart/form-data">
        <apex:pageBlock >
            <apex:pageBlockSection showHeader="false" columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="File Name" for="fileName" />
                    <input type="text" name="File-Name" value="" />
                    <input type="hidden" name="Mime-Type" value="application/pdf" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="File" for="file" />
                    <input name="File" type="file" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <button>Submit</button>
            </apex:pageBlockButtons>
        </apex:pageBlock>                 
    </form>  
    <div id="warehouseList"></div>
    <script type="text/javascript">
        $("form#data").submit(function(){

            var formData = new FormData($(this)[0]);

            $.ajax({
                url: 'https://api.na1.echosign.com/api/rest/v5/transientDocuments',
                type: 'POST',
                headers: {'Access-Token' :'3AAABLblqZhCG8fU-alud8zcDf-N8I68P7yfB6nIi6U_PcCQsnobiSYzIpfzmgYaCMnl2PwcuVlSbk7yS_uNV8BkF8ymc3f2m'},
                data: formData,
                async: false,
                success: function (data) {
                    SendAgreementExt.send(data);
                },
                cache: false,
                contentType: false,
                processData: false
            });

            return false;
        });
    </script>
    {!LeadMessage}
</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.