I have been using EncodingUtil.base64Encode() ; to get base64 contentversion data aiming to send/upload file from salesforce to outside server like AWS S3 which was working fine but due to sfdc governor limits unable to use more than 6MB file.

list<ContentVersion> vds = [SELECT Id,VersionData FROM ContentVersion WHERE Id=:cvId limit 1];
     b = vds[0].VersionData;
     eb = EncodingUtil.base64Encode(b);

So I am re developing alternative approach by JS in visualforce and to get VersionData base64 data I am trying to sf ContentVersion object API /services/data/v58.0/sobjects/ContentVersion/'+cvId+'/VersionData which returns special character sets, I guess it is BLOB but BASE64 (as per salesforce manual).

enter image description here

How to convert this type of data sets to BASE64?

and with that I have tried to upload file directly to external Server like AWS S3 but it uploaded with corrupted data, so I learned that it might need convert VersionData to encoded base64, so for that I have tried to used btoa() / encodeURIComponent() in JavaScript Code. but no luck, the output file seems corrupted.

So I assumed that if I can find alternative Apex method EncodingUtil.base64Encode() in JavaScript it might solve it.

My current JS Code snippet..

 var url = window.location.origin + '/services/data/v58.0/sobjects/ContentVersion/'+cvId+'/VersionData';
 var xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.setRequestHeader('Authorization', 'Bearer '+ sessionId);
       xhr.onreadystatechange = function() {
            if (xhr.readyState == XMLHttpRequest.DONE) {  
                var vblob  = xhr.response;                
**//Here need to Encode/Decode Base64**
                var blobBase64= (( btoa(encodeURIComponent(vblob)) ));
//Convert to Blob
        var fileUrl = "data:" + ContentType + ";base64," + bytesBase64;
        const response = await fetch(fileUrl, {});
        const conBlob = await response.blob();

//conBlob this data is uploading to external API


If anyone have similar experience please reply your thought of solution.


  • you wanna upload file in sf or somewhere else? Oct 11, 2023 at 12:46
  • yes already mentioned that to upload file from salesforce to external Server like AWS S3 Oct 11, 2023 at 13:09

1 Answer 1


I have resolved by using fetch () with response.blob()..

  var fileUrl = window.location.origin + '/services/data/v58.0/sobjects/ContentVersion/'+cvId+'/VersionData';
        const response = await fetch(fileUrl, {
            headers: {
                'Authorization': 'Bearer '+ sessionId
        const conBlob = await response.blob();
  • I'm glad you solved your problem. However, we do have some rules around here. Instead of saying [RESOLVED], like you might on some forums, simply check the checkbox on the left of your answer to mark it as the best answer.
    – sfdcfox
    Oct 12, 2023 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .