Skip to content

Class: UtilsMedia ​

Media utilities: conversions between base64-encoded data and binary formats (Blob, ArrayBuffer) usable by the browser.

Remarks ​

Available everywhere via k.Utils.Media. All methods are static.

Example ​

ts
// Turn a base64 image into a downloadable Blob
const blob = k.Utils.Media.base64ToBlob(imageBase64, 'image/png');
const url = URL.createObjectURL(blob);

Constructors ​

Constructor ​

ts
new UtilsMedia(): UtilsMedia;

Returns ​

UtilsMedia

Methods ​

base64ToArrayBuffer() ​

ts
static base64ToArrayBuffer(base64): ArrayBuffer;

Converts a base64 string into an ArrayBuffer.

Parameters ​

base64 ​

string

The base64 data (without data-URL prefix).

Returns ​

ArrayBuffer

The corresponding ArrayBuffer.

Example ​

ts
const buffer = k.Utils.Media.base64ToArrayBuffer(audioBase64);

See ​

base64ToBlob to obtain a Blob.


base64ToBlob() ​

ts
static base64ToBlob(
   base64, 
   contentType, 
   sliceSize?): Blob;

Converts a base64 string into a Blob object.

Parameters ​

base64 ​

string

The base64 data. Any data-URL prefix (e.g. data:image/png;base64,) is automatically stripped.

contentType ​

string

The MIME type of the blob (e.g. 'image/png', 'image/jpeg').

sliceSize? ​

number = 512

Size of the chunks processed during conversion (bytes).

Returns ​

Blob

The corresponding Blob.

Default Value ​

512

Example ​

ts
const blob = k.Utils.Media.base64ToBlob(canvas.toDataURL(), 'image/png');

See ​

base64ToArrayBuffer to obtain an ArrayBuffer.