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["body"] = array("messageSegments"=>array("type"=>"text","text"=>"Please accept this receipt."));
$data["attachment"] = array("attachmentType"=>"NewFile","description"=>"Receipt for expenses","title"=>"receipt.png");
$data["feedElementType"] = "FeedItem";
$data["subjectId"] = "a0Eo00000019X2jEAE";
$requestJson = "[".json_encode($data)."]";
$requestJson = '{"body": {"messageSegments": [{"type": "Text","text": "Please accept this receipt."}]},
"attachment": {"attachmentType": "NewFile","description": "Receipt for expenses","title": "receipt.png"}}';
$boundary = "MIME_boundary";
$CRLF = "\r\n";
// The complete POST consists of an XML request plus the binary image separated by boundaries
$firstPart = '';
$firstPart .= "--" . $boundary . $CRLF;
$firstPart .= 'Content-Disposition: form-data; name="json"' . $CRLF;
$firstPart .= 'Content-Type: application/json;charset=utf-8' . $CRLF . $CRLF;
$firstPart .= $requestJson;
$firstPart .= $CRLF;
$firstPart .= "--" . $boundary . $CRLF;
$firstPart .= 'Content-Disposition: form-data; name="feedItemFileUpload"; filename="receipt.png"' . $CRLF;
$firstPart .= "Content-Transfer-Encoding: binary" . $CRLF;
$firstPart .= "Content-Type: application/octet-stream;charset=ISO-8859-1" . $CRLF . $CRLF;
$firstPart .= $multiPartImageData;
$firstPart .= $CRLF;
$firstPart .= "--" . $boundary . "--" . $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 "<pre>";
print_r($error);
print_r(($response));
}
=========Above code converted into Titanium Code AS=========
var requestJson = {
"body" : {
"messageSegments" : [{
"type" : "Text",
"text" : "Please accept this receipt."
}]
},
"attachment" : {
"attachmentType" : "NewFile",
"description" : "Receipt for expenses",
"title" : "receipt.png"
}
// "feedElementType" : "FeedItem",
// "subjectId" : args.data.Store__r.Id
};
//===========
var content = '';
// var boundary = 'a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq';
var boundary = 'MIME_boundary';
var CRLF = "\r\n";
content += '--' + boundary + '\r\n';
content += 'Content-Disposition: form-data; name="json"' + CRLF;
content += 'Content-Type: application/json; charset=utf-8' + CRLF + " " + CRLF;
content += requestJson;
content += CRLF;
content += '--' + boundary + "" + CRLF;
content += 'Content-Disposition: form-data; name="feedItemFileUpload"; filename="receipt.png"' + CRLF;
content += 'Content-Transfer-Encoding: binary"' + CRLF;
content += 'Content-Type: application/octet-stream; charset=ISO-8859-1' + CRLF + "" + 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("POST”,”url will be here”);
xhr.setTimeout(15000);
xhr.setRequestHeader("Authorization", Alloy.Globals.userinfo.token_type + " " + Alloy.Globals.userinfo.access_token);
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
xhr.setRequestHeader("Accept", "application/json");
xhr.send(send_data);