You need to sign in to do that
Don't have an account?
Rajath Kundapur 17
LWC for Mutiple File upload
I have created a Component, Which consists of custom objects and file upload fields. Upon saving clicking a save multiple files and a record must be saved.
In my scenario, only a single file is saving.
@AuraEnabled
public static Email_Sending__c saveFile(Map<string, string> emailRecord, String strFileName, String base64Data)
{
Email_Sending__c emSend=new Email_Sending__c();
emSend.Email__c=emailRecord.get('Email');
emSend.Message__c=emailRecord.get('Email_Message');
emSend.Sent_Status__c=emailRecord.get('Email_Status');
emSend.Subject__c=emailRecord.get('Subject');
emSend.RecordId__c = emailRecord.get('RecordId');
insert emSend;
base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
if(strFileName != '' && base64Data != '')
{
ContentVersion cv = new ContentVersion();
cv.Title = strFileName;
cv.PathOnClient = '/' + strFileName;
cv.FirstPublishLocationId =emSend.Id;
cv.VersionData = EncodingUtil.base64Decode(base64Data);
cv.IsMajorVersion = true;
Insert cv;
}
return emSend;
}
JS Code
handleFilesChange(event) {
if(event.target.files.length > 0) {
this.filesUploaded = event.target.files;
this.fileName = event.target.files[0].name;
}
}
uploadHelper(paramStatus) {
if(this.filesUploaded.length > 0)
{
this.file = this.filesUploaded[0];
if (this.file.size > this.MAX_FILE_SIZE) {
window.console.log('File Size is to long');
return ;
}
this.fileReader= new FileReader();
this.fileReader.onloadend = (() => {
this.fileContents = this.fileReader.result;
let base64 = 'base64,';
this.content = this.fileContents.indexOf(base64) + base64.length;
this.fileContents = this.fileContents.substring(this.content);
});
this.fileReader.readAsDataURL(this.file);
}
this.contactRecord['Email'] = this.storeRecords.toString();
this.contactRecord['Email_Message'] = this.emailMessage;
this.contactRecord['Subject'] = this.emailSubject;
this.contactRecord['Email_Status'] = paramStatus;
this.contactRecord['RecordId'] = this.recordId;
if(this.filesUploaded.length > 0)
this.saveToFile();
else
this.saveToWithoutFile();
}