function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prajapati.LakhanPrajapati.Lakhan 

Image upload using multipart/form-data

Hi,

 

I have to upload an image to third party storage server using REST API, It does not support 'base64' content-transfer-encoding and salesforce does not allow to send binary content using string ( I guess), because when we convert blob into string, it gets converted to ascii characters. Is there any way to convert blob into byte [ ] or binary stream to post binary content.

 

Here is my sample code for the requirement. -

 

 

<apex:form>
   <apex:inputFile value="{!fileContent}" fileName="{!name}" fileSize="{!size}"/>
   <apex:commandButton value="Upload" action="{!uploadImage}"/>
</apex:form>

/************Controller Code*************/

Public Blob fileContent {get;set;}
Public Integer size {get;set;}
public String name {get;set;}

String body = '-----------------------------153501500631101\r\n';
body = body + 'Content-Disposition: form-data; name="Filedata"; filename="testImg.png"\r\n';
body = body + 'Content-Type: image/png\r\n\r\n';
body = body + fileContent.toString() + '\r\n';
body = body + '-----------------------------153501500631101--\r\n';

 

 

Any help will be apriciated. Thanks in anvance....

 

Lakhan

 

 

 

paul-lmipaul-lmi
i had a similar use case in the past. apex doesn't support HTTP callouts with multi-part form data.
Prajapati.LakhanPrajapati.Lakhan

Thanks Paul for the prompt reply.

 

It works fine with text/csv content, but I need to upload an image, is there any way in salesforce to convert base64 into binary content with encoding?

 

Thanks

Lakhan

SuperfellSuperfell

No, its not possible to do this.

EnreecoEnreeco

Try this solution: http://enreeco.blogspot.it/2013/01/salesforce-apex-post-mutipartform-data.html

I've messed with Blob, Encoding, Decoding and HttpResponse

foundation_devfoundation_dev

I'm having similar problem:

I am trying to make HTTP POST request with multipart/form-data, the problem is that the request is sent but without the body part. I was trying the solution: http://enreeco.blogspot.it/2013/01/salesforce-apex-post-mutipartform-data.html, but it doesn't worked for me. I think it is the same problem as: http://boards.developerforce.com/t5/Apex-Code-Development/Image-upload-using-multipart-form-data/m-p/243335/highlight/true#M42610. Will this issue be solved? Is there any alternative to HttpRequest?

Pooja Kadam 4Pooja Kadam 4
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm