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
- Create Apex class which will save record in record as attachement
- Create Lightning Component to upload documents
- 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
17 comments
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.
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
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
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
HI Dhanik,
Does this work in android mobile? Will we be able to upload multiple files after taking multiple photos?
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
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?
Hello Ahmed,
Please try to debug the issue. On the basis of debug error, I can help you.
Thank You,
Dhanik
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}
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
I’ve just copy pasted this code from your blog and didnt change anything as this code is generic right.
Hello Ahmed,
I have fixed the code issue in fileUploaderHelper.js. Please take the updated code and try again.
Thank You,
Dhanik
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
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.
Hello Lokesh,
Please explain, Which attribute declaration is not defined.
Thank You,
Dhanik
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 🙂
Hello Mariana,
This component can be used when you have a record already created. A record is required to upload files.
Thank You,
Dhanik