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
Prakash GBPrakash GB 

lightning:fileupload

Hi,

i have requirement to upload a file clicking on a link on lightning component. is it possible to fire lightning:fileupload from controller.js?

Thanks
Prakash GB
ajay Duggi(Heptarc)ajay Duggi(Heptarc)
Dear Prakash,

The below code for fileUpload which i have tested and working fine.
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="accept" type="List" default="['.jpg', '.jpeg', '.png']"/>
    <aura:attribute name="multiple" type="Boolean" default="true"/>
    <aura:attribute name="disabled" type="Boolean" default="false"/>
    
    <lightning:fileUpload label="Add attachment" multiple="{!v.multiple}" 
                          accept="{!v.accept}" recordId="{!v.recordId}" 
                          onuploadfinished="{!c.handleUploadFinished}" />
</aura:component>
 
({
    handleUploadFinished : 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!",
            "message": "File "+fileName+" Uploaded successfully."
        });
        toastEvent.fire();
        
        $A.get('e.lightning:openFiles').fire({
            recordIds: [documentId]
        });        
    }
})

1. Open setup and choose object to add a component button, before go to developer console to create lightning component and add above code with controller.JS
User-added image
2.Click on right corner toggle button to see a fileupload label which we added in buttons,links and actions.
User-added image
3.Now you can upoad file and get a status is success.
User-added image

I hope it may help to resolve the issue.
Prakash GBPrakash GB
Hi Ajay,

i appreciate your effort but your answer will not help me to achieve my requirement.
i need to show a link on lightning component not fileuploader component. 

Thanks
Prakash GB
ajay Duggi(Heptarc)ajay Duggi(Heptarc)
Dear Prakash,
As per my understand you want link on page, If Yes
There can be no upload if recordId is null when we don't provide a valid recordId, the fileUpload component automatically disabled.

If No please send me the screenshot where you are going to upload that would help to me understand.
Prakash GBPrakash GB
Arjun,

I don't want to keep "Upload file" thing on my component. it  should be just like some url (example : add doc (http://google.com)). when clicking on that should ask user to choose file from his local drive.
my idea to achieve this is using a lightning:fileupload component and hiding it. clicking on the URL should fire the ightning:fileupload component.

Thnaks
Prakash GB