Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • 10 Salesforce Chrome Extensions to Boost Your Productivity
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Monday, June 9
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Upload Multiple Files in Lightning Component

    Upload Multiple Files in Lightning Component

    Dhanik Lal SahniBy Dhanik Lal SahniApril 1, 2020Updated:May 12, 202017 Comments2 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Upload Multiple Files in Lightning Component
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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

     

    file upload lightning salesforce
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleCOVID India Real Data In Lightning Community
    Next Article COVID19 Live Chart in Lightning Web Component
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni9 Mins Read

    10 Salesforce Chrome Extensions to Boost Your Productivity

    June 1, 2025
    By Dhanik Lal Sahni4 Mins Read

    How to Build a Generic Modal Window in Lightning Web Component

    May 26, 2025
    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    View 17 Comments

    17 Comments

    1. Srikanth Dabbakuti on 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 on 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
    2. Ankit on 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 on 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
    3. Yug on 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 on 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
    4. Ahmed on 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 on 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 on 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 on 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 on 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 on 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 on 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

    5. Lokesh on 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 on September 14, 2022 7:13 pm

        Hello Lokesh,

        Please explain, Which attribute declaration is not defined.

        Thank You,
        Dhanik

        Reply
    6. Mariana on 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 on 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 Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • 10 Salesforce Chrome Extensions to Boost Your Productivity
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code analysis (3) code optimization (8) custom metadata types (5) design principle (9) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (65) lightning-combobox (5) lightning-datatable (10) lightning component (31) Lightning web component (63) lwc (52) named credential (8) news (4) optimize apex code (4) optimize apex trigger (3) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (142) salesforce apex (47) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.