import { Meta, Source } from '@storybook/addon-docs/blocks'

<Meta title='@dxtmisha/en/functional-basic/Functions/blobToBase64 - Converting Blob to Base64'/>

# `blobToBase64`

Asynchronously converts `Blob` or `File` objects into a Base64 string (Data URL).

**Parameters:**
- `blob: Blob` — The `Blob` (or `File`) object to be converted.
- `clean: boolean = false` — If `true`, removes the Data URL prefix (e.g., `data:image/png;base64,`).

**Environment Support:**
The utility automatically detects the environment:
- **Browser**: Uses `FileReader` to read the content.
- **Server (SSR)**: Uses `Buffer` and the asynchronous `blob.arrayBuffer()` method.

**Returns:**
`Promise<string | undefined>` — Base64 string or `undefined` if conversion is not possible.

## Examples

<Source
  language='typescript'
  code={`
import { blobToBase64 } from '@dxtmisha/functional-basic'

// 1. Default (returns Data URL)
const dataUrl = await blobToBase64(file)
// 'data:image/png;base64,iVBORw0KGgo...'

// 2. Raw Base64 (without prefix)
const rawBase64 = await blobToBase64(file, true)
// 'iVBORw0KGgo...'

// 3. Handling API unavailability
const result = await blobToBase64(blob)
if (result) {
  // work with string
}
  `}
/>
