Hey guys, In this post we are going to learn about how to upload files in Salesforce lightning component.
In this example we will attached the files from Custom sObject by help of wrapper apex class in lightning component.
Files we used in this post example:-
File Name | File Type | Description |
---|---|---|
uploadAttachmentCmp.cmp | Lightning Component | It is used to user interface display upload button and toast success message on browser. |
uploadAttachmentCmpConroller.js | JavaScript Controller File | It is used for client side javascript function that is communicate to apex class method. |
uploadFileController.apxc | Apex Class Controller | Upload the file into database server |
Custom Object:- childObjTrigger__c
|
Custom Object | Upload the attachment files on custom sObject Files & Attechment section. |
Final Output
Other related post that would you like to learn in LWC
Note:- Custom sObject >> childObjTrigger__c
You need to change custom sObject name with your custom sObject name.
Step 1:- Create Lightning Component : uploadAttachmentCmp.cmp
From Developer Console >> File >> New >> Lightning Component
uploadAttachmentCmp.cmp [Lightning Component File]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<aura:component controller="uploadFileController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" > <aura:attribute name="wrapListItems" type="childObjTrigger__c[]"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <aura:attribute name="accept" type="List" default="['.jpg', '.jpeg','.pdf']"/> <aura:attribute name="multiple" type="Boolean" default="true"/> <aura:attribute name="disabled" type="Boolean" default="false"/> <div class="slds"> <table class="slds-table slds-table--bordered slds-table--col-bordered" style="border-collapse:collapse;"> <thead> <tr> <th>Attachment</th> </tr> </thead> <tbody> <aura:iteration items="{!v.wrapListItems}" var="childObjVar" indexVar="index"> <tr id="{!childObjVar.recordId}"> <td> <lightning:fileUpload multiple="{!v.multiple}" accept="{!v.accept}" recordId="{!childObjVar.recordId}" onuploadfinished="{!c.uploadFileAction}" disabled="{!v.disabled}"/> </td> </tr> </aura:iteration> </tbody> </table> </div> </aura:component> |
Step 2:- Create Lightning Component : uploadAttachmentCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
uploadAttachmentCmpController.js [JavaScript Controller]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
({ doInit : function(component, event, helper) { var action = component.get("c.childObjWrapMethod"); action.setCallback(this, function(response){ var state = response.getState(); if(state=='SUCCESS'){ var result = response.getReturnValue(); component.set('v.wrapListItems',result); } }); $A.enqueueAction(action); }, uploadFileAction : function(component, event, helper) { var uploadedFiles = event.getParam("files"); var documentId = uploadedFiles[0].documentId; var fileName = uploadedFiles[0].name; var toastEvent = $A.get("e.force:showToast"); toastEvent.setParams({ "title": "Success!", "type": "success", "message": "File "+fileName+" Uploaded successfully." }); toastEvent.fire(); $A.get('e.force:refreshView').fire(); }, }) |
Step 3:- Create Lightning Application : uploadFileController.apxc
From Developer Console >> File >> New >> Apex Class
uploadFileController.apxc [Apex Class Controller]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class cloneFilesFromOldCaseToNew { public class wrapperChildObjClass{ @AuraEnabled public Id recordId; public wrapperChildObjClass(Id recordId){ this.recordId = recordId; } } @AuraEnabled public static List<wrapperChildObjClass> childObjWrapMethod(){ List<wrapperChildObjClass> wrapObj = new List<wrapperChildObjClass>(); List<childObjTrigger__c> childObjList = [Select Id, Name From childObjTrigger__c limit 1]; for(childObjTrigger__c childObjWrp: childObjList){ wrapObj.add(new wrapperChildObjClass(childObjWrp.Id)); } return wrapObj; } } |
Further post that would you like to learn
nice its very helpfull…!!!!!
Good Information about the topic
In this blog I get a great post sir please also visit my blog I post great content
your post is nice & I really loved reading your post.
Great Thanks for sharing awesome info. It was very well authored and easy to understand.
Great Info Topic
5 Internet Approval Site (Do Follow Backlinks)