Ionic FileReader:DOMException,无法将blob转换为base64字符串

ggazkfy8  于 2022-12-08  发布在  Ionic
关注(0)|答案(1)|浏览(220)

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

k4aesqcs

k4aesqcs1#

为什么你要这样做?我建议你把文件分成块

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

相关问题