1

I am trying to upload large amount of file using File reader.

The current code is working for file till 150 MB only.

I came across this method 'readAsArrayBuffer' which can read large amount of files. But when tried using this method not able to read data as its throwing memory out space exception in browser.

private ProcessReadFile(file:File){
  return new Promise<void>(resolve=>{
     const reader=new FileReader();
     let content:any;
     reader.onloadend=()=>{
             content=reader.result;'
             const filecontent=content.split(base64,')[1];
             this.fileupload.push({
                 Filecontent:filecontent,
                 FileName=file.Name
             });
          resolve();
       };
    reader.readAsDataURL(file);
   });
}

With the above code able to upload file till 150 MB.

I tried with reader.readAsArrayBuffer but getting memory buffer size. I tried with the below approach to convert array buffer to base64 string and it worked. But its working when i first upload large file (around 600MB file) and when i tried to upload another large files(200MB) file its throwing "Out of Memory" browser error.

 private ProcessReadFile(file:File){
   return new Promise<void>(resolve=>{
        const reader= new FileReader();
        let content:any;
        reader.onloadend=()=>{
                     content=reader.result;
                     var fileContent=_arrayBufferToBase64(content);
                    this.fileUpload.push({
                       Filecontent:fileContent;
                       FileType:file.Type
                     });
                 resolve;
         };
     });
  }

Method used to convert array buffer to base 64 string. Please let me know if there is there is size limitation for window.btoa() or is it broswer cache issue.

   function _arrayBufferToBase64( buffer ) {
        var binary = '';
        var bytes = new Uint8Array( buffer );
        var len = bytes.byteLength;
        for (var i = 0; i < len; i++) {
            binary += String.fromCharCode( bytes[ i ] );
        }
        return window.btoa( binary );
    }

Please let me know how can I resolve this issue Is there anything Array buffer needs to get free after usage. So that's the reason the second time its uploaded its throwing error .

AMDI
  • 895
  • 2
  • 17
  • 40
  • What happens with `readAsDataURL`? You call to String.fromCharCode will hit the max number of arguments, but I'd expect your readAsDataURL to work until at least 375MB files (over that the returned string would get over 500MB which is [the max limit a string can have in V8](https://stackoverflow.com/questions/61271613/chrome-filereader-api-event-target-result#)). – Kaiido Feb 21 '22 at 06:17
  • readAsDataURL is giving content as null at this line const filecontent=content.split(base64,')[1];.Please let me know if we can achieve if there is any other way – AMDI Feb 21 '22 at 16:06

1 Answers1

0

I had the same problem with 200MB files. In my case, as there is an API responsible for receiving the files, so I decided to split the file in 2. That is, split the string in 2 and send 1 to 1. because angular's fileReader component itself doesn't accept this limit. I hope it helps.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 29 '22 at 20:17