Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. <apex:form id="theform">
  2. Name <apex:inputText value="{!name}" id="name" label="Fisrt Name" /><br />
  3. Phone <apex:inputText value="{!phone}" id="phone" label="Last Name" /><br />
  4. Website <apex:inputText value="{!website}" id="website" label="website" /><br />
  5. <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}" />
  6. <apex:commandButton value="Upload" action="{!sendJSON}" styleClass="btn btn-primary" />
  7. </apex:form>
  8.  
  9. public void sendJSON(){
  10.  
  11. String encodedBody = EncodingUtil.base64Encode(attachment1.Body);
  12. String requestUrl;
  13. requestUrl = 'https://xxxxx-developer-edition.ap1.force.com/services/apexrest/Account/';
  14. String Body;
  15. Body = '{"name" : "' + name + '","phone" : "' + phone + '","website" : "' + website + '","fileUploadName" : " ' + attachment1.Name + '","fileUploadBody" : "' + attachment1.Body + '"}';
  16. system.debug('!!! apiform: ' + Body);
  17.  
  18. Http h = new Http();
  19. HttpRequest req = new HttpRequest();
  20. HttpResponse res=new HttpResponse();
  21. req.setTimeout(120000);
  22. req.setEndpoint(requestUrl);
  23. req.setHeader('Content-Type','application/json');//Set the Proper Header
  24. req.setHeader('Accept','application/json');
  25. req.setBody(Body);//JSON body as String
  26. req.setMethod('POST');
  27.  
  28. try{
  29. res=h.send(req);
  30.  
  31. finalJSON2 = res.getBody();
  32. system.debug('!! ' + res.getBody());
  33. system.debug('!! getStatusCode ' + res.getStatusCode());
  34. if(res.getStatusCode() != 201 && res.getStatusCode() != 200 ){
  35. finalJSON2 = 'error != to 200's: ' + String.valueOf(res.getStatusCode());
  36. //log the error
  37. }else{
  38. finalJSON2 = res.getBody();
  39. }
  40.  
  41. }catch (Exception e){
  42. finalJSON2 = 'Error !!! ' + e;
  43. }
  44. //return res.getBody();
  45. }
  46.  
  47. @HttpPost
  48. global static String doPost(String phone, String website, String other, String name, String fileUploadName, String fileUploadBody) {
  49. Account account = new Account();
  50. account.Name = name;
  51. account.phone = phone;
  52. account.website = website;
  53. insert account;
  54.  
  55. Attachment attachment1 = new Attachment();
  56. attachment1.ParentId = account.Id;
  57. attachment1.Body = Blob.valueOf(fileUploadBody);
  58. attachment1.Name = fileUploadName;
  59.  
  60. system.debug('!!! ' + fileUploadBody); // debug: {Body=Blob[49616], Name=lead tab.jpg}
  61. insert attachment1;
  62. return account.Id;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement