You need to sign in to do that
Don't have an account?
SF_32
Unable to upload multiple files in lwc
Hi Everyone,
I want to upload upload multiple files by chunking and converting to base 64 in LWC.
I tried looping them by helper function but no luck.
I want to upload upload multiple files by chunking and converting to base 64 in LWC.
I tried looping them by helper function but no luck.
handleUpload(){ if(this.selectedFilesToUpload.length > 0) { this.showSpinner = true; this.file = this.selectedFilesToUpload[0]; this.fileReaderObj = new FileReader(); this.fileReaderObj.onloadend = (() => { let fileContents = this.fileReaderObj.result; fileContents = fileContents.substr(fileContents.indexOf(',')+1) let sliceSize = 1024; let byteCharacters = atob(fileContents); let bytesLength = byteCharacters.length; let slicesCount = Math.ceil(bytesLength / sliceSize); let byteArrays = new Array(slicesCount); for (let sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) { let begin = sliceIndex * sliceSize; let end = Math.min(begin + sliceSize, bytesLength); let bytes = new Array(end - begin); for (let offset = begin, i = 0 ; offset < end; ++i, ++offset) { bytes[i] = byteCharacters[offset].charCodeAt(0); } byteArrays[sliceIndex] = new Uint8Array(bytes); } this.myFile = new File(byteArrays, this.fileName, { type: this.fileType }); let reader = new FileReader(); reader.onloadend = (() => { let base64data = reader.result; this.base64FileData = base64data.substr(base64data.indexOf(',')+1); this.fileUpload(); }); reader.readAsDataURL(this.myFile); }); this.fileReaderObj.readAsDataURL(this.file); } else { this.fileName = 'Upload any file'; } }
I found in the documentation that there is a component called lightning-file-upload which has multiple attribute can you try using it in the code and see if it works.
>> The link to the documentation is: https://developer.salesforce.com/docs/component-library/bundle/lightning-file-upload/documentation
I hope this helps and in case if this comes handy can you please choose this as the best answer so that it can be used by others in the future.
Regards,
Anutej.