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
Rui LopesRui Lopes 

How to attach a file to a record that will be created with lightning component?

I would like to attach a file to a expense record that i will create though a lightning component. I dont have recordId so I think I need to create the record for the attachment first. can you guys please help me out? this is my code:

Component
<aura:component controller="ExpensesController" implements="force:appHostable,flexipage:availableForAllPageTypes" access="global" >
    <aura:handler name="init" value="this" action="{!c.doInit}" />

    <aura:attribute name="expensefields" type="String[]" default="['Expense_type__c','Amount__c','BTW__c',
    'Opmerking__c', 'Approver_1__c','Approver_2__c']" />

    <lightning:notificationsLibrary aura:id="notifLib"/>

    <lightning:card iconName="standard:document" title="Add Attachment">
        <div class="slds-card__body slds-card__body_inner">
            <lightning:input name="file1" type="file" label="Attachment"
                             multiple="true" accept="image/png, .zip" onchange="{!c.handleFilesChange}"/>
        </div>
    </lightning:card>
    <lightning:card iconName="standard:account" title="Add Expense">
        <div class="slds-card__body slds-card__body_inner">
            <lightning:recordForm
                    objectApiName="Expense__c"
                    fields="{!v.expensefields}"
                    onsuccess="{!c.handleSuccess}"
            />
        </div>
    </lightning:card>
</aura:component>

Controller 
({
    doInit: function(component, event, helper){

    },

    handleSuccess: function (component, event, helper) {
        helper.handleSuccess(component,event);
    },

    handleFilesChange: function (component, event, helper) {
        helper.handleFilesChange(component,event)
    }
})

Helper
({
    handleSuccess : function(component, event) {
        component.find('notifLib').showToast({
            "variant": "success",
            "title": "Expense Created",
            "message": "Record ID: " + event.getParam("id")
        });
    },

    handleFilesChange: function (component, event) {
        var uploadedFiles = event.getParam("files");
        alert("Files uploaded : " + uploadedFiles.length);
    }
})

No Apex controller at this moment.
MagulanDuraipandianMagulanDuraipandian
Use lightning:fileUpload and use recordId in in it.
Sample Code - http://www.infallibletechie.com/2018/03/sample-code-component.html