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

inputfile: binary data instead of base64... how to?



I have a webservice which only accepts binary data. However on Salesforce, we get the file encoded in base64 (which can be converted to hexa but that does not help me much). Up to now, I didn't find a way to convert this base64 to literal bytes. Doing a "toString()" to the Blob helps only if the file is a text file, but not a picture, an application or anything else.

May someone point me on the right direction?


If it does not work with an apex:inputfile, would it be possible to use the real html component <input type="file" ...> in order to get the real file in binary data and to bypass the Salesforce encoding? But I did not find a way to send the file to the controller :(


Any help would be appreciated!



Does your server accept Content-Encoding? You can set the Content-Encoding attribute, and as long as your server honors it (not necessarily the web service), it should correctly decode the binary data. This is done using:



If that fails, you may consider using "Content-Transfer-Encoding" instead; most web servers will recognize one or both of the headers (you shouldn't use both in a single request, though), often before the web service is even initiated.


Strings in Apex Code are the same as in Java; they are UTF-8, and not much you do about it will result in any sort of string you can send in raw binary. I've been told by a developer that binary "strings" are not supported; at the very least, CR, LF, and CRLF may be mangled, and characters outside the valid UTF-8 namespace may generate errors.


The Blob format passed in through apex:inputFile is in binary format. However, there's no mechanism to get it out of the blob and have it survive the trip in pure binary format; it needs to be converted to a UTF-8 string, or a base64 encoded mechanism. I've never found a way to make the conversion possible.


the server accepts neither content-encoding not the content-transfer-encoding... the only possibility is unfortunately only with binary datas.


But thank you very much for the explanation, it confirms me what I thought and it's now much more clear why this is not working with the javascript encoding.