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
Chie MenChie Men 

attach multiple files using filereader

Hi, 

I have a code that attaches multiple files to a Case but encounter an issue with files that's 200KB and above.


Here's the code that reads multiple files and save it as an attachment to a Case.


for (var i = 0; i < files.length; i++){        
    //console.log('looping  file...' + files[i].file.name);
    (function(file) {
                     
        var reader = new FileReader();                                  
        reader.onload = function (e) {
            console.log('reader.onload ..' + file.name);
            var byte64 = reader.result.replace('data:', '').replace(/^.+,/, '');
            var Attachment = new sforce.SObject("Attachment");    
            Attachment.Name = file.name;                        
            Attachment.ContentType = file.type;
            Attachment.ParentID =  caseId;
            Attachment.Body =  byte64;    
            Attachment.IsPrivate = false;
             
            var insertResult = sforce.connection.create([Attachment]);
            //console.log("insertResult=" +insertResult);
            
        };            
                            
        reader.readAsDataURL(file);    
        
                
    })(files[i].file);     
}


I don't have any issue attaching 1 large file up to 25MB. But when I attached multiple files,  I can only attach up to 200KB. 
For example, I have 2 files.
File A - 186 KB and 
File B - 568 KB. 

There are times,  that I can attach 186KB and 568KB but most of the time, it's only File A that is successfully attached. The code skips File B.  I removed that code that creates the attachment and I notice that the console log -- console.log('reader.onload ..' + file.name); -- is printed. This was not printed when sforce.connection.create([Attachment]); is present.

Any idea on this?
Khan AnasKhan Anas (Salesforce Developers) 
Hi Chie,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://anilsfgeek.blogspot.com/2017/12/lightning-compent-to-insert-multiple.html

https://salesforce.stackexchange.com/questions/238796/for-multi-file-upload-same-file-being-uploaded-twice

http://hellosnl.blogspot.com/2017/08/lightning-component-for-attaching-files.html

http://sfdcmonkey.com/2017/09/25/file-upload-lightning-component

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Chie MenChie Men
Hi, Khan.  I am using classic. Thanks!