Home > Back-end >  FileReader: DOMException, Unable to convert blob to base64 String
FileReader: DOMException, Unable to convert blob to base64 String

Time:08-09

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

CodePudding user response:

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 ;
}
  • Related