Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <apex:form id="theform">
- Name <apex:inputText value="{!name}" id="name" label="Fisrt Name" /><br />
- Phone <apex:inputText value="{!phone}" id="phone" label="Last Name" /><br />
- Website <apex:inputText value="{!website}" id="website" label="website" /><br />
- <apex:inputFile value="{!attachment1.body}" filename="{!attachment1.Name}" id="theAttachment" accept="image/jpeg,image/jpg, image/png, application/pdf, application/rtf, image/gif, image/bmp, image/tiff, image/tif, application/vnd.oasis.opendocument.text, application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" filesize="{!att0size}" />
- <apex:commandButton value="Upload" action="{!sendJSON}" styleClass="btn btn-primary" />
- </apex:form>
- public void sendJSON(){
- String encodedBody = EncodingUtil.base64Encode(attachment1.Body);
- String requestUrl;
- requestUrl = 'https://xxxxx-developer-edition.ap1.force.com/services/apexrest/Account/';
- String Body;
- Body = '{"name" : "' + name + '","phone" : "' + phone + '","website" : "' + website + '","fileUploadName" : " ' + attachment1.Name + '","fileUploadBody" : "' + attachment1.Body + '"}';
- system.debug('!!! apiform: ' + Body);
- Http h = new Http();
- HttpRequest req = new HttpRequest();
- HttpResponse res=new HttpResponse();
- req.setTimeout(120000);
- req.setEndpoint(requestUrl);
- req.setHeader('Content-Type','application/json');//Set the Proper Header
- req.setHeader('Accept','application/json');
- req.setBody(Body);//JSON body as String
- req.setMethod('POST');
- try{
- res=h.send(req);
- finalJSON2 = res.getBody();
- system.debug('!! ' + res.getBody());
- system.debug('!! getStatusCode ' + res.getStatusCode());
- if(res.getStatusCode() != 201 && res.getStatusCode() != 200 ){
- finalJSON2 = 'error != to 200's: ' + String.valueOf(res.getStatusCode());
- //log the error
- }else{
- finalJSON2 = res.getBody();
- }
- }catch (Exception e){
- finalJSON2 = 'Error !!! ' + e;
- }
- //return res.getBody();
- }
- @HttpPost
- global static String doPost(String phone, String website, String other, String name, String fileUploadName, String fileUploadBody) {
- Account account = new Account();
- account.Name = name;
- account.phone = phone;
- account.website = website;
- insert account;
- Attachment attachment1 = new Attachment();
- attachment1.ParentId = account.Id;
- attachment1.Body = Blob.valueOf(fileUploadBody);
- attachment1.Name = fileUploadName;
- system.debug('!!! ' + fileUploadBody); // debug: {Body=Blob[49616], Name=lead tab.jpg}
- insert attachment1;
- return account.Id;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement