I am trying to retrieve VersionData with a standard REST API call from a VisualForce Page a then upload it to a S3 bucket but end up with corrupted files. I believe this is an encoding problem on the client side as plain text or CSVs work like a charm. I can upload VersionData from Apex but there are heap and request size limitations so I need to send the data from the client.

enter image description here This is the call and returned string, I tried changing mimeType and encoding on both sides (SF call and S3 upload) with no luck.

Any ideas how to properly process images or pdf files? Thanks a lot


Files should not be treated as strings. I solved the problem by leaving jQuery's ajax as I did not succeed to request a response type. By switching to plain JS and making a GET call (XMLHttpRequest) with response type set to blob I received an Object, instead of a string. Base64 string can be then retreived using JS FileReader.

  • The image on the left is the retrieved data? It's been a while since I've read a GIF file, but it looks about right to me. The only problem is that your code is choosing to show it as plain text instead of as an image; you might just be missing an img tag. What happens if you render it as a link? Can you click on it to download/open it? Does it look right?
    – sfdcfox
    Jun 26 '18 at 6:35
  • Hi, thanks for reply! Yes that is the data - I just appended that string to the DOM as plain text to see what it looks like. To display/download it I would need to encode it to base64 first, right? That's the trouble - I am not sure how to encoded the retrieved data as some of those characters are not legal latin chars.
    – Petr Vav
    Jun 26 '18 at 7:33

The problem was jQuery ajax method.

XMLHttpRequest with responseType set to "blob" solved the problem.


The result is a binary blob. Appending it the way you did obviously results in garbled output, because you're looking at a raw set of bytes, not characters in a string. Given your current code, you should be able to do something like this:

  '<img src="data:image/gif;base64,"+btoa(result)+'" />');

Your browser should take care of the rest for you.

  • btoa cannot be used as the response contains non-Latin1 characters. However, overriding mimeType to use different encoding produces "readable" and encodable string but it seems to corrupt the data.
    – Petr Vav
    Jun 26 '18 at 8:03
  • 1
    @PetrVav Well, I do know you should not be playing with encoding. Encoding is a feature of text streams. Binary streams do not know the concept of encoding. I think that's where you're going down the wrong path.
    – sfdcfox
    Jun 26 '18 at 8:06
  • I agree, but do you have any other idea how to process the response? btoa cannot be applied on the response as it is and other "custom" base64 encoding methods I tried produce corrupted data.
    – Petr Vav
    Jun 26 '18 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.