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 

Uploading large files failing (5mb+)

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 need to upload at least 10 MB.

Thanks,
Sumant K

 
NagendraNagendra (Salesforce Developers) 
Hi Suman,

Unfortunately, your SOL when it comes to that sort of limit, you're only route is going to be moving the operation off Salesforce.

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.

For more information please check with below links which might help you. Mark this as solved if the information helps.

Regards,
Nagendra.
Adheena jacob 1Adheena jacob 1
Hi Sumant,
Did you ever fixed this? I am facing similar issue. Could you  please help.
Suman KuchSuman Kuch
Adheena,
Unfortunately I couldn't upload morethan 5 MB with Apex so I had to write javascript code in Visualsource and used XMHttpRequest to upload file, this allowing me to upload upto 1GB file. Salesforce will not care if you use javascript. 
 
Mujtaba Saleforce DevMujtaba Saleforce Dev
Hi Suman
Can you please share the code which you used to upload the file to google drive will really appreciate if you can guide me in this reference.