I have a lwc page where users can upload files in an object. I have a requirement the import button need to be enabled only if the file is not uploaded yet , but if it is uploaded succesfully i should get only the file uploaded visible .
With my code it make the uploaded button disabled before that i get a file uploaded .
Below is the my lwc code:
<lightning-file-upload
name="fileUploader"
accept={acceptedFormats}
record-id={recordId}
onuploadfinished={handleUploadFinished}
onchange={saveFile}
disabled={disableImport}
multiple
>
</lightning-file-upload>
@track inputFiles;
get disableImport() {
return !this.inputFiles || this.inputFiles.files.length === 0;
}
saveFile(event){
this.inputFiles = event.target;
var fileCon = this.filesUploaded[0];
this.fileSize = this.formatBytes(fileCon.size, 2);
if (fileCon.size > MAX_FILE_SIZE) {
let message = 'File size cannot exceed ' + MAX_FILE_SIZE + ' bytes.\n' + 'Selected file size: ' + fileCon.size;
this.dispatchEvent(new ShowToastEvent({
title: 'Error',
message: message,
variant: 'error'
}));
return;
}
var reader = new FileReader();
var self = this;
reader.onload = function() {
var fileContents = reader.result;
var base64Mark = 'base64,';
var dataStart = fileContents.indexOf(base64Mark) + base64Mark.length;
fileContents = fileContents.substring(dataStart);
self.upload(fileCon, fileContents);
};
reader.readAsDataURL(fileCon);
}