Are you uploading file in vf page? you need to just use inputFile tag. More info @ https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_inputFile.htm
Use inputFile in VF page, in controller get reference to the uploaded 'Document' in controller. Now you have base64 encoded body of the document. Make POST webservice call to thirdparty system.
you need to just use inputFile tag.
More info @ https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_inputFile.htm
Hi Ashish
I have to upload image from salesforce to another external system(ONE HUB).
Here is good link of same question - https://developer.salesforce.com/forums/?id=906F00000008ycEIAQ
http://blog.enree.co/2013/01/salesforce-apex-post-mutipartform-data.html
http://developer.force.com/cookbook/recipe/uploading-a-document-using-visualforce-and-a-custom-controller
-Thanks
Ashlekh Gera
Hi Ashlejh,
I have worked on those links But enree post is not working for the Images..
Now you have base64 encoded body of the document. Make POST webservice call to thirdparty system.
Can you please post your code here so we can see.
I've use Apex:inpuFile and able to upload file in saleforce pdf and jpg both files was uploaded.
-Thanks
Ashlekh Gera
<apex:page controller="Onehubcntr">
<apex:form >
<apex:pageblock title="Get & Post the File in ONE HUB">
<apex:inputText value="{!GetFile1}" title="GetFile1"/>
<br/>
<br/><apex:inputFile style="width:100%" id="fileToUpload" value="{!fileBody}" filename="{!fileName}" />
<apex:commandbutton action="{!getSFDCSessionID}" value="PUTFILE"/>
</apex:pageblock>
<iframe src="data:image/gif;base64,{!Pdffile}"></iframe>
</apex:form>
</apex:page>
Apex :
public class Onehubcntr{
public string fileName{get;set;}
public Blob fileBody{get;set;}
public static string attachid;
//Fetched from URL
String code ;
public String GetFile1{get;set;}
public static String AccessToken1;
public transient String Pdffile{get;set;}
public Onehubcntr (){
code = ApexPages.currentPage().getParameters().get('code') ;
//Get the access token once we have code
if(code != '' && code != null){
system.debug('>>>>>>>>>>>>>>>>.....code'+code);
getOneHubToken();
}
}
public class WrapperAuth{
String access_token{get;set;}
String token_type{get;set;}
String uid{get;set;}
public WrapperAuth(){
}
}
public pagereference getSFDCSessionID(){
PageReference pg = new PageReference('https://ws-api.onehub.com/oauth/authorize?response_type=code&client_id=8roys5vt6f5lbbkkih2x2g50dijp3u0&client_secret=5eoy8diy38wou36hettaj8k4enrl2gv&username=venkatanagarajas@gmail.com&password=Spiderman@57&redirect_uri=https://c.ap2.visual.force.com/apex/OnehubPage') ;
return pg ;
}
public void getOneHubToken(){
String tokenuri = 'https://ws-api.onehub.com/oauth/token?grant_type=authorization_code&client_id=8roys5vt6f5lbbkkih2x2g50dijp3u0&redirect_uri=https://c.ap2.visual.force.com/apex/OnehubPage&code='+code;
//String tokenuri = 'https://ws-api.onehub.com/oauth/token?grant_type=password&client_id=8roys5vt6f5lbbkkih2x2g50dijp3u0&client_secret=5eoy8diy38wou36hettaj8k4enrl2gv&redirect_uri=https://c.ap1.visual.force.com/apex/DropboxIntegration&code='+code;
HttpRequest req = new HttpRequest();
req.setEndpoint(tokenuri);
req.setMethod('POST');
req.setTimeout(60*1000);
Blob headerValue = Blob.valueOf('8roys5vt6f5lbbkkih2x2g50dijp3u0' + ':' + '5eoy8diy38wou36hettaj8k4enrl2gv');
System.debug('headerValue====>>>>' + headerValue);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
System.debug('authorizationHeader======>>>>>>' + authorizationHeader);
req.setHeader('Authorization', authorizationHeader);
Http h = new Http();
String resp;
HttpResponse res = h.send(req);
resp = res.getBody();
string accessToken;
system.debug('Response=====>>>>'+resp);
if(res.GetStatusCode()== 200){
JSONParser parser = JSON.createParser(resp);
parser.nextToken();
parser.nextValue();
String fieldName = parser.getCurrentName();
accessToken= parser.getText();
uploadFile(accessToken);
}
System.debug(' You can parse the response to get the access token ::: ' + resp);
}
public static void uploadFile(string accessToken){
// public void getFile(string accessToken){
System.debug('accessToken=====>>>>>'+accessToken);
attachment file =[select id ,Name,body from attachment where id=:'00P28000001KGiw'];
//String boundary = '----663207250.1446661634481';
String boundary = '----------------------------741e90d31eff';
//String boundary = accessToken;
String header = '--'+boundary+'\nContent-Disposition: form-data; name="file.id"; filename="'+file.name+'";\nContent-Type: application/octet-stream';
String footer = '--'+boundary+'--';
String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
while(headerEncoded.endsWith('='))
{
header+=' ';
headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
}
String bodyEncoded = EncodingUtil.base64Encode(file.body);
Blob bodyBlob = null;
String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
if(last4Bytes.endsWith('==')) {
last4Bytes = last4Bytes.substring(0,2) + '0K';
bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
} else if(last4Bytes.endsWith('=')) {
last4Bytes = last4Bytes.substring(0,3) + 'N';
bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
footer = '\n' + footer;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
} else {
footer = '\r\n' + footer;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
req.setMethod('POST');
req.setEndpoint(' https://ws-api.onehub.com/workspaces/848421/folders/873366451/files');
req.setBodyAsBlob(bodyBlob);
req.setTimeout(120000);
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug('Response====>>>>'+res);
system.debug('ResponseBody=====>>>>>'+res.getBody());
}
}