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
Karthik jKarthik j 

How to get recordId after file uploading

Component Code
<lightning:fileUpload label="Upload Multiple files" 
                               multiple="false" 
                              accept=".pdf, .png, .jpg"
                              recordId="{!v.recordId}"
                              aura:id="multipleUpload"
                             onuploadfinished="{!c.handleUploadFinished}" />

JScontroller
handleUploadFinished : function(component, event, helper) {        
   
        var uploadedFiles = event.getParam('files'); 
        //set action to call updatePicturePath method from Server-side controller
        var action = component.get('c.updatePicturePath');
        //set parametrs
        console.log('Id>>'+component.get('v.recordIdforFile'));
        var recordId=component.get('v.recordId);
        alert(recordId);
        action.setParams({
            recId : recordId
        }); 
        action.setCallback(this, function(response){
            var state = response.getState();
            alert(state);
            if(state === 'SUCCESS' || state === 'DRAFT') {
                var resultToast = $A.get("e.force:showTest");
                resultToast.setParams({
                    "title" : "Success!",
                    "message" : uploadedFiles.length + "file uploadedsuccessfully!"
                });
                resultToast.fire();;
            }
        });
        $A.enqueueAction(action);
    }

I am getting undefined for recordId and I need it to send as a parameter to updatePicturePath APEX method.
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Karthik,

>> https://developer.salesforce.com/docs/component-library/bundle/lightning:fileUpload/documentation

As mentioned in the above link you can make use of onuploadfinished event that gets triggered after the files are uploaded.

I am adding the piece of snippet along with the information that I found would be useful in the above link:

You must handle the onuploadfinished event, which is fired when the upload is finished.
 
({
    handleUploadFinished: function (cmp, event) {
        // Get the list of uploaded files
        var uploadedFiles = event.getParam("files");
        alert("Files uploaded : " + uploadedFiles.length);

        // Get the file name
        uploadedFiles.forEach(file => console.log(file.name));
    }
})

event.getParam("files") returns a list of uploaded files with the properties name and documented.

>> name: The file name in the format filename.extension, for example, account.jpg.
>> documentId: The ContentDocument Id in the format 069XXXXXXXXXXXX.
>> ContentVersionId: The ContentVersion Id in the format 068XXXXXXXXXXXX.

Note: When a guest user uploads files, the returned list includes only the name and ContentVersionId. The documentId is not returned.

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.  

Thanks.
Suraj Tripathi 47Suraj Tripathi 47
Hi,
<aura:attribute name="recordId" type="String" />

<lightning:fileUpload label="Attach receipt"
        name="fileUploader"
        multiple="true"
        accept="{!v.filetype}"
        recordId="{!v.recordId}"
        onuploadfinished="{!c.handleUploadFinished}" />

Your recordId will be saved in attribute name recordId.

Please Mark it as the best answer if it helps you.

Thanks & regards
Suraj Tripathi