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
LaurentDelcambreLaurentDelcambre 

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:
http://peterknolle.com/file-upload-lightning-component/

(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?

Cheers,
Laurent
jiforcejiforce
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

 @future
 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) 

@AuraEnabled 
 public static void dummy(){ }

Then the uploadChunk method is something like below:

uploadChunk: function(cmp, file, fileName, fileContents, fromPos, toPos, attachId) {
        alert('uploadTest');
        // 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);
    
            action.setParams({
                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); 
                    }else{
                       alert('finishing');
                    }
                }
                if(state == "ERROR"){
                     alert('Error to upload document');
                }
            });         
            $A.enqueueAction(action); 
        });               
        $A.enqueueAction(action);   
    }

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.

Cheers,
Ji
jiforcejiforce

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

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

https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Lightning&criteria=ALLQUESTIONS&id=9060G000000I30IQAS

​Thank You.