Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Image Uploading to salesforce API

$
0
0

Hi I am working on app where we want to upload image to salesforce chatter api.Backend engineer provided me php code where he did uploading.I true to do same but no luck.Plse help me out here.

PHP Code

<?php

if($_POST['testNow']){ $image = $_FILES['image']; if($_FILES['image']['tmp_name']){ @move_uploaded_file($_FILES['image']['tmp_name'],"receipt.png"); }

$file      = 'receipt.png';       // image file to read and upload
$picNameIn = 'feedItemFileUpload';
$handle = fopen($file,'r');         // do a binary read of image
$multiPartImageData = fread($handle,filesize($file));
fclose($handle);

$data[&quot;body&quot;] = array(&quot;messageSegments&quot;=&gt;array(&quot;type&quot;=&gt;&quot;text&quot;,&quot;text&quot;=&gt;&quot;Please accept this receipt.&quot;));
$data[&quot;attachment&quot;] = array(&quot;attachmentType&quot;=&gt;&quot;NewFile&quot;,&quot;description&quot;=&gt;&quot;Receipt for expenses&quot;,&quot;title&quot;=&gt;&quot;receipt.png&quot;);
$data[&quot;feedElementType&quot;] = &quot;FeedItem&quot;;
$data[&quot;subjectId&quot;] = &quot;a0Eo00000019X2jEAE&quot;;
$requestJson = &quot;[&quot;.json_encode($data).&quot;]&quot;;

$requestJson = '{&quot;body&quot;: {&quot;messageSegments&quot;: [{&quot;type&quot;: &quot;Text&quot;,&quot;text&quot;: &quot;Please accept this receipt.&quot;}]},
                 &quot;attachment&quot;: {&quot;attachmentType&quot;: &quot;NewFile&quot;,&quot;description&quot;: &quot;Receipt for expenses&quot;,&quot;title&quot;: &quot;receipt.png&quot;}}';

$boundary = &quot;MIME_boundary&quot;;
$CRLF = &quot;\r\n&quot;;

// The complete POST consists of an XML request plus the binary image separated by boundaries
$firstPart   = '';
$firstPart  .= &quot;--&quot; . $boundary . $CRLF;
$firstPart  .= 'Content-Disposition: form-data; name=&quot;json&quot;' . $CRLF;
$firstPart  .= 'Content-Type: application/json;charset=utf-8' . $CRLF . $CRLF;
$firstPart  .= $requestJson;
$firstPart  .= $CRLF;

$firstPart .= &quot;--&quot; . $boundary . $CRLF;
$firstPart .= 'Content-Disposition: form-data; name=&quot;feedItemFileUpload&quot;; filename=&quot;receipt.png&quot;' . $CRLF;
$firstPart .= &quot;Content-Transfer-Encoding: binary&quot; . $CRLF;
$firstPart .= &quot;Content-Type: application/octet-stream;charset=ISO-8859-1&quot; . $CRLF . $CRLF;
$firstPart .= $multiPartImageData;
$firstPart .= $CRLF;
$firstPart .= &quot;--&quot; . $boundary . &quot;--&quot; . $CRLF;

$fullPost = $firstPart;

$ch  = curl_init();
$url = $_POST['url']; ;

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fullPost);

//00Do0000000Hb0Q!ARkAQGqQ5MSsbusSdSyPXTE1ZuRkcUpB36PRCYrnOrNE8vpKK3kLOnUdM2foiG1zU9EBjKl8I8Aog34UHmAmrpBFdDeu5d_s
$session = $_POST['session'];

$headers = array();
$headers[] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
$headers[] = 'Host: na17.salesforce.com';
$headers[] = 'Content-Length: ' . strlen($fullPost);
$headers[] = 'Authorization: OAuth '.$session;
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);

$response = curl_exec($ch);
$error    = curl_error($ch);

print &quot;&lt;pre&gt;&quot;;
print_r($error);
print_r(($response));

}

=========Above code converted into Titanium Code AS=========

     var requestJson = {
                &quot;body&quot; : {
                         &quot;messageSegments&quot; : [{
                                    &quot;type&quot; : &quot;Text&quot;,
                                    &quot;text&quot; : &quot;Please accept this receipt.&quot;
                         }]
                },
                &quot;attachment&quot; : {
                         &quot;attachmentType&quot; : &quot;NewFile&quot;,
                         &quot;description&quot; : &quot;Receipt for expenses&quot;,
                         &quot;title&quot; : &quot;receipt.png&quot;
                }
                // &quot;feedElementType&quot; : &quot;FeedItem&quot;,
                // &quot;subjectId&quot; : args.data.Store__r.Id
     };

     //===========
     var content = '';
     // var boundary = 'a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq';
     var boundary = 'MIME_boundary';
     var CRLF = &quot;\r\n&quot;;
     content += '--' + boundary + '\r\n';
     content += 'Content-Disposition: form-data; name=&quot;json&quot;' + CRLF;
     content += 'Content-Type: application/json; charset=utf-8' + CRLF + &quot; &quot; + CRLF;
     content += requestJson;
     content += CRLF;
     content += '--' + boundary + &quot;&quot; + CRLF;
     content += 'Content-Disposition: form-data; name=&quot;feedItemFileUpload&quot;; filename=&quot;receipt.png&quot;' + CRLF;
     content += 'Content-Transfer-Encoding: binary&quot;' + CRLF;
     content += 'Content-Type: application/octet-stream; charset=ISO-8859-1' + CRLF + &quot;&quot; + CRLF;
     var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'storeimage.jpg');
     var full_content = Ti.createBuffer({
                value : content
     });
     var ?leStream = Ti.Stream.createStream({
                source : file.read(),
                mode : Titanium.Stream.MODE_READ
     });
     var content_size = full_content.append(Ti.Stream.readAll(?leStream));
     Ti.API.info(file.getSize() + ' :SubjectId: Appended File Size : ' + content_size);
     content = CRLF;
     content += '--' + boundary + '--' + CRLF;
     full_content.append(Ti.createBuffer({
                value : content
     }));
     var send_data = full_content.toBlob();

     var xhr = Ti.Network.createHTTPClient();
     xhr.onload = function(res) {
                Ti.API.info('onload: ' + JSON.stringify(res));
     };
     xhr.onerror = function(error) {
                Ti.API.info('onerror: ' + JSON.stringify(error));
     };
     xhr.open(&quot;POST”,”url will be here”);
     xhr.setTimeout(15000);
     xhr.setRequestHeader(&quot;Authorization&quot;, Alloy.Globals.userinfo.token_type + &quot; &quot; + Alloy.Globals.userinfo.access_token);
     xhr.setRequestHeader(&quot;Content-Type&quot;, &quot;multipart/form-data; boundary=&quot; + boundary);
     xhr.setRequestHeader(&quot;Accept&quot;, &quot;application/json&quot;);
     xhr.send(send_data);

In above code block all "(double quat) get displayed like &quot;


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>