Skip to main content

readBlob

Types

function readBlob<GReadType extends IFileReaderReadType>(
blob: Blob,
readType: GReadType
): IObservable<IReadBlobObservableNotifications<GReadType>>
interface IFileReaderFormatsToTypeMap {
'data-url': string;
'text': string;
'array-buffer': ArrayBuffer;
}

type IFileReaderReadType = keyof IFileReaderFormatsToTypeMap;

Definition

Creates an Observable able to read a Blob.

You may select the output type with IFileReaderFormatsToTypeMap.

The Observable emits values in the form of Notifications:

  • next: the data of the Blob
  • complete: when reading is done
  • error: if an error occurred
  • progress: when reading => IProgress
caution

The FileReader slowly becomes legacy. Instead, you may prefer to use fromReadableStream:

fromReadableStream(blob.stream());

Example

Read a Blob as an ArrayBuffer

const blob = new Blob([new Uint8Array([0, 1, 2, 3])]);
const subscribe = readBlob(blob, 'array-buffer');

subscribe((notification) => {
console.log(notification.name, ':', notification.value);
});

Output:

progress: { loaded: 2, total: 4 }
next: ArrayBuffer([0, 1, 2, 3])
complete