2

Im using the FileReader to convert a Blob (Created from a file thats greater then 2GB) into a base64 string. But sadly I'm ending up with the following error:

DOMException: The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired

It works fine if the file is below 2GB.

Converter:

  private convertBlobToBase64 = (blob: Blob, filename: string) => new Promise<FileForSaveInDevice>((resolve, reject) => {
    console.log('Blob', blob);
    const reader = new FileReader();
    reader.onerror = reject;
    reader.onload = () => {
      resolve({ base64: reader.result as string, filename });
    };
    reader.readAsDataURL(blob);
  });

I found a simmelar question, which didn't get answered: FileReader: DOMException, Unable to convert blob to arrayBuffer again and again

maxi hraschan
  • 153
  • 12
  • [The maximum length a string can be in V8 is 512MB](https://stackoverflow.com/questions/61271613/chrome-filereader-api-event-target-result), base64 will generate a string about 34% bigger than the original data. So I'm quite surprised you were able to get a 2GB file this way... Or was it using Firefox? Also, why do you think you need a data URL? In 99.9% of the case what you want is to keep the binary data as binary. If you wish to fetch this file e.g in an HTML element like a – Kaiido Jul 29 '22 at 09:06
  • I actually only tried it with a file that was bigger then 2GB and a file which was around 200-300MB. But i will try to use a blob uri. – maxi hraschan Aug 01 '22 at 07:16
  • @Kaiido Your advice fixed my issue. I just used the Blob without converting. Thanks :) – maxi hraschan Aug 01 '22 at 07:44
  • Yes! Worked for me as well :) – maxi hraschan Aug 08 '22 at 07:24

1 Answers1

3

Why you have to do that? I suggest u to divide file in chunks

blobInChunks(blob:Blob) {
    let chunkSize = 1024 * 1024 * 64; //64Mb
    let chunks = Math.ceil(blob.size/chunkSize) ;
    let chunk = 0 ;

    let _chunksArray:Blob[] = [] ;
  
    while (chunk++ <= chunks) {
        let offset = chunk*chunkSize ;
        _chunksArray.push(blob.slice(offset, offset + chunkSize)) ;
    }

    return _chunksArray ;
}
Talon
  • 351
  • 1
  • 11