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
Suman KuchSuman Kuch 

How can we decode large String to blob (EncodingUtil.base64Decode)?

Hi,
We are replacing native functionality of Attachments to upload files to "Box"external storage using Rest API, I was able to upload small files upto 5MB but when trying to upload big files I get the error in Apex code (not even going to box) 
System.StringException: String length exceeds maximum: 6000000
This is occurig when trying to decoding ( base64Decode ) from String blob before Rest API call, please check the code
String bodyEncoded = EncodingUtil.base64Encode(fileBody);
Blob bodyBlob = null;
String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
// GW: replacement section to get rid of padding without corrupting data
if(last4Bytes.endsWith('==')) {
    last4Bytes = last4Bytes.substring(0,2) + '0K';
    bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
    String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
    bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded); //Here its failing
}else if(last4Bytes.endsWith('=')){
    last4Bytes = last4Bytes.substring(0,3) + 'N';
    bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
    footer = '\n' + footer;
    String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
    bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);              
}else{
    footer = '\r\n' + footer;
    String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
    bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);  
}
Error is triggering at "bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);" this line.
Please advice how can we ignore the size of the file? We may have to upload 10MB files sometimes, please advice

Thanks,
Sumant K

 
SandhyaSandhya (Salesforce Developers) 
Hi,

Apex has a limit to keep the value up to 6 MB, so it can't cross the value else it will throw the exception.

One option would be to create a heroku app with a single endpoint that takes the attachment Id. The endpoint can then retrieve the attachment and then upload it to the 3rd party server.

Please refer below link for the same.

http://salesforce.stackexchange.com/questions/29141/string-length-exceeds-maximum-6000000
 
Hope this helps you!

Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya