+ Start a Discussion

Heap size issue when uploading a file in Lightning app

HI everyone,

We are trying to create our own file upload Lightning component.
We based our approach on what Peter Knolle did:

(Quick summary: we upload the fill in chunks and update the Attachment body with DML because we cannot pass a String of size bigger than 6 000 000)

Our problem is that line:
a.Body = EncodingUtil.base64Decode(EncodingUtil.base64Encode(a.Body)+ base64Data);
It seems to load the existing body twice in memory, once for a.Body and once for EncodingUtil.base64Decode(...) which makes the biggest file we can upload only around 1.5MB because of the Heap Size limit.

I tried using "transient" variables to try and unload memory. 
I also tried setting some variable to null or "" after using them but the Heap Size usage stays the same.
No luck.

Any idea?

Hi Laurent,

I am running into same issue.

I have 1 solution to solve the problem, and the maximum file size to upload can be a bit over 4M.

Heap size limited for synchronous function is 6M while it is 12M for asynchronous fuction.

Any function inside   action.setCallback(this, function(a) { } is running asynchronously

so my solution is to call 

uploadChunk (cmp, file, fileName, fileContents, fromPos, toPos, attachId) 

inside action.setCallback(this, function(a) { }, and then make server side function as an asynchronous function

 public static void appendToFile(Id fileId, String base64Data)

To make it to be called in action.setCallback(this, function(a) { }

I created a method in controller(maybe there is a better way to setup and envoke action.setCallback) 

 public static void dummy(){ }

Then the uploadChunk method is something like below:

uploadChunk: function(cmp, file, fileName, fileContents, fromPos, toPos, attachId) {
        // call this dummy fuction just for setting up action.setCallback
        var action = cmp.get("c.dummy"); 
        action.setCallback(this, function(a) {
            var action = cmp.get("c.saveFile"); 
            var chunk = fileContents.substring(fromPos, toPos);
                parentId: cmp.get("v.OrgInfo.Id"),
                fileName: fileName, // customised name
                base64Data: encodeURIComponent(chunk), 
                fileDescription: file.name,
                contentType: file.type,
                fileId: attachId
            var self = this;
            action.setCallback(this, function(a) {
                var state = a.getState();
                if(state == "SUCCESS"){
                    attachId = a.getReturnValue();
                    fromPos = toPos;
                    toPos = Math.min(fileContents.length, fromPos + self.CHUNK_SIZE); 
                    // recrusive method 
                    if(fromPos < toPos) {
                        self.uploadChunk(cmp, file, fileName, fileContents, fromPos, toPos, attachId); 
                if(state == "ERROR"){
                     alert('Error to upload document');

Also I believe using chunk instead of encodeURIComponent(chunk)

then in controller remove base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8'); can also increase a bit upload file size.

encodeURIComponent probably adds more characters into the content...

Hope this helps.


 public static void appendToFile(Id fileId, String base64Data) needs to be 

 @future @AuraEnabled 
 public static void appendToFile(Id fileId, String base64Data)
Muzammil BajariaMuzammil Bajaria
I need help in uploading a file in contentVersion using lightning component. Please refer to below link for details. 


​Thank You.