function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SF_32SF_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.
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';
        }
    }

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi there,

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.