I have lightning-input tag with type as file in Child component, and I am making use of this child component in Parent component which contains Submit button. So when the user clicks on submit , file should be uploaded.
fileUploadChild.html
<template>
<div class="slds-m-around_medium">
<lightning-input type="file"
accept=".xlsx, .xls, .csv, .png, .doc, .docx, .pdf"
label="Upload Files" onchange={openfileUpload} ></lightning-input>
</div>
</template>
fileUploadChild.js
import { LightningElement,track,api } from 'lwc';
export default class FileUploadChild extends LightningElement {
@api recordId;
@track fileNames;
@track fileData;
openfileUpload(event) {
const file = event.target.files[0]
var reader = new FileReader()
reader.onload = () => {
var base64 = reader.result.split(',')[1]
this.fileData = {
'base64': base64,
'filename': file.name,
}
console.log('file data'+this.fileData)
const customEvent = new CustomEvent("filedatachild",{
detail: JSON.stringify(this.fileData)
});
this.dispatchEvent(customEvent)
}
reader.readAsDataURL(file)
console.log('file'+file.name);
}
}
I am sending the file info through events.
Now how do I handle the event in Parent and submit the file?