Home Salesforce Upload Multiple Files in Lightning Component

Upload Multiple Files in Lightning Component

by Dhanik Lal Sahni

Many time we get requirement to uploading multiple files using lightning component. This post give detail about how to upload multiple files.

Steps to complete this requirement

  1. Create Apex class which will save record in record as attachement
  2. Create Lightning Component to upload documents
  3. Add above created component in page to test it

1. Create Apex class to save record

we need to create apex class to save selected files in record as attachment. Files which is uploaded first time will be created as attachment. Already uploaded files will be update with new contents.

Apex Code:

2. Create Lightning Component

Create Lightning component which will upload files in current record. Current record means it will take record id of record where this component will be uploaded. If component is added on case record, then it will take current case record id. If component is added on account record, then it will take current account record id and same for other object as well. We have to implement interface force:hasRecordId to use current record id.

implements="force:hasRecordId"

We have to declare recordId attribute like this.

 <aura:attribute name="recordId" type="Id" />

We can add toast to show message or error while uploading files.

showMessage : function(message,isSuccess) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            "title": isSuccess?"Success!":"Error!",
            "type":isSuccess?"success":"error",
            "message": message
        });
        toastEvent.fire();
    }

We can use lightning:input for uploading multiple files

<lightning:input aura:id="fileId" onchange="{!c.handleFilesChange}" type="file" name="file" label="" multiple="true"/>

We can get numbers of files uploaded using below script in controller/helper class. Based on files count we can upload files in loop.

var fileCount=component.find("fileId").get("v.files").length;

Complete Lightning Component Code


3. Testing Created Lightning Component

References: https://developer.salesforce.com/docs/component-library/bundle/lightning:input

 

You may also like

17 comments

Srikanth Dabbakuti February 14, 2021 - 12:52 pm

Hi, Why are we restricting the file size here. I we remove the file size and try to upload then the files are not loading in salesfroce.

Reply
Dhanik Lal Sahni February 14, 2021 - 9:16 pm

Hello Srikanth,

When we create custom file upload, file will be saved using apex. Apex has heap size limit of 6MB. So we have to restrict file upload less then 6MB. See document for reference Apex Heap Size

Thank You,
Dhanik

Reply
Ankit November 22, 2021 - 10:31 am

Can we upload more than 25 files as Salesforce file upload limit is 24 max?
We are looking for custom file uploader, so just wanted to check that is possible with custom file uploader

Reply
Dhanik Lal Sahni November 25, 2021 - 2:11 pm

Hey Ankit,

You can get all the attached files and you can divide all files into chunks of 10 files. Upload 10 files at a time. This way, you can do it in a custom uploader.

Alternatively, you can log a case to increase the file limit. See this help document.

Thank You,
Dhanik

Reply
Yug December 8, 2021 - 10:39 am

HI Dhanik,

Does this work in android mobile? Will we be able to upload multiple files after taking multiple photos?

Reply
Dhanik Lal Sahni December 8, 2021 - 7:31 pm

Hey Yug,

I have not tried this on mobiles. You can check it and if an issue in the component then we can connect and resolve it.

Thank You,
Dhanik

Reply
Ahmed May 5, 2022 - 9:44 pm

Hello Dhanik,

I’ve tried this solution and its working. that is the files are getting uploaded but once I click on upload button it just shows the loading spinner and doesn’t show the message file uploaded successfull.. what could be the issue?

Reply
Dhanik Lal Sahni May 6, 2022 - 10:37 am

Hello Ahmed,

Please try to debug the issue. On the basis of debug error, I can help you.

Thank You,
Dhanik

Reply
Ahmed May 6, 2022 - 12:49 pm

Hey Dhanik, this is the error I get on the screen while I click upload button. But let me tell you that files were getting uploaded but its just that the screen shows uploading text and loading icon and not the message files uploaded successfully.

Error–>

This page has an error. You might just need to refresh it. Error in $A.getCallback() [helper is not defined] Callback failed: apex://FileUploadController/ACTION$saveFile Failing descriptor: {markup://c:ClientUploadCMP}

Reply
Dhanik Lal Sahni May 6, 2022 - 2:31 pm

Hello Ahmed,

Looks like you are not passing helper in your js function parameter. Add parameter helper in your js function and it should resolve your issue.

Thank You,
Dhanik

Reply
Ahmed May 6, 2022 - 3:32 pm

I’ve just copy pasted this code from your blog and didnt change anything as this code is generic right.

Dhanik Lal Sahni May 7, 2022 - 2:12 pm

Hello Ahmed,

I have fixed the code issue in fileUploaderHelper.js. Please take the updated code and try again.

Thank You,
Dhanik

Ahmed May 10, 2022 - 1:59 pm

Hey Dhanik, I too made the same changes, still it shows the same error. I want to connect with you for the same. I’ve added you in linkedin. Please accept the connection

Lokesh September 10, 2022 - 3:42 pm

Hi,

In fileuploadercontroller.js file line no-4 you are trying to fetch the files without declaration of attribute, then how can you fetch the files can you please explain me.

Reply
Dhanik Lal Sahni September 14, 2022 - 7:13 pm

Hello Lokesh,

Please explain, Which attribute declaration is not defined.

Thank You,
Dhanik

Reply
Mariana October 24, 2022 - 10:10 pm

Hi!

Can we use the component in a form where we are creating a record? Or, for this case, the component would have to come if the record was already created, right?

Thanks 🙂

Reply
Dhanik Lal Sahni October 30, 2022 - 9:11 am

Hello Mariana,

This component can be used when you have a record already created. A record is required to upload files.

Thank You,
Dhanik

Reply

Leave a Comment