I have an LWC component which takes xlsx/xlsm file as input and sends the file data to an API as input. Initially, I used lightning-input with type=file which is working as expected for files <3MB and I was unable to work with files >3MB. So I decided to use lightning-file-upload here.
HTML file
<lightning-file-upload
accept=".xlsx, .xlsm"
multiple={setmultiplefiles}
label="Insert File"
onuploadfinished={openfileUpload}>
</lightning-file-upload>
JS File
openfileUpload(event) {
console.log(event.target.files);
console.log(event.target.files[0]);
const file = event.target.files[0];
var reader = new FileReader()
reader.onload = () => {
var base64 = reader.result.split(',')[1]
this.fileData = {
'filename': file.name,
'base64': base64,
'recordId': this.recordId
}
}
reader.readAsDataURL(file);
}
For lightning-file-upload: I see [object Object] in first console log and fails with error "cannot read properties of undefined (reading '0')". After removing the second log statement, I'm getting this error "[Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob']" exactly in the last line(reader.readAsDataURl(file);) and
For lightning-input: (Working fine) I see [object FileList] in console log.
I have referred many related solutions and nothing seems to work. Please provide any solution or suggest any other alternatives to accomplish this task.