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
kranjankranjan 

Ability to concatenate the Blob with String in APEX to upload image as multi-part data

Hi,

 

I want to do a multi-part post from Apex to salesforce itself for an image. And salesforce expects binary data for the image. However we need to prefix the header with boundary as part of multipart convention and append footer so that receiving end (salesforce in this case) can parse the same and process the data(Image in this case). The issue is image is a blob and headers and footers are string. And we cannot directly concatenate these 3 parts in Apex to send to SF.

 

One possible way suggested by people who had similar issues was use base64encoded to convert blob to string and concatenate the header and footer. This is good and works but only 33% of times. Reason is base64 encdoing appends "=" or "==" sign to padd at the end if the source blob data bytes are not exact multiple of 3 (This is how base64encoding works). So these padded characters creates issues if they are appended while encoding. There is no fruitful way i am able to figure out to resolve this final issue and I get 400 bad request by SF. There was a way suggested by @Enreeco in his blog but that resolves the error from 400 to a successful 201 return value. But the background issue is that the way the "=" characters were removed actually corrupted the blob data, so receiving end receives a valid request but NOT a valid content and thus image is not really uploaded and assigned to the profile.

 

To avoid the above concatenation problem, I also tried to set Content-Transfer-Encoding as "base64" and sending only the image as base64encoded and header/ footers as normal string, however that request is also generating 500 server error.

Has anyone faced any similar issues or has any pointers for any workaround either for concatenating the header, footer string to blob body or else for resolving the padded "=" characters and not corrupting the data. Or has anybody successfully tried using base64 as Content-Transfer-Encoding method to send multi-part post? Any help is highly appreciated.

 

Thanks in advance

 

Kamal

ForceMantis (Amit Jain)ForceMantis (Amit Jain)
Will it work if you convert header and footer that are strings to Blob, combine three Blob values and post it as a single Blob value or base64Encode it for submission if that works. I have posted solution to combine multiple Blob values into a single value on salesforce stackexchange (http://salesforce.stackexchange.com/questions/133380/how-to-combine-multiple-blob-in-apex-to-get-single-blob-value/133381)