Skip to content

useBase64

Category
Export Size
794 B
Last Changed
last year

Reactive base64 transforming. Supports plain text, buffer, files, canvas, objects, maps, sets and images.

Demo

Text Input
Base64
Buffer Input
new ArrayBuffer(1024)
Base64
File Input
Base64
Image Input
Base64

Usage

ts
import { 
useBase64
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
text
=
shallowRef
('')
const {
base64
,
promise
,
execute
} =
useBase64
(
text
)

Supported Input Types

  • string - Plain text
  • Blob - File or blob data
  • ArrayBuffer - Binary data
  • HTMLCanvasElement - Canvas element
  • HTMLImageElement - Image element
  • Object / Array / Map / Set - Serialized to JSON

Return Values

PropertyDescription
base64The resulting base64 string
promiseThe promise of the current transformation
executeManually trigger the transformation

Data URL Format

By default, the output is in Data URL format (e.g., data:text/plain;base64,...). Set dataUrl: false to get raw base64.

ts
const { 
base64
} = useBase64(text, {
dataUrl
: false })
// Returns raw base64 without the data URL prefix

Canvas and Image Options

When transforming canvas or image elements, you can specify the MIME type and quality.

ts
const 
canvas
=
document
.
querySelector
('canvas')
const {
base64
} = useBase64(
canvas
, {
type
: 'image/jpeg', // MIME type
quality
: 0.8, // Image quality (0-1, for jpeg/webp)
})

Custom Serializer

For objects, arrays, maps and sets, you can provide a custom serializer. Otherwise, the data will be serialized using JSON.stringify (maps are converted to objects, sets to arrays).

ts
const 
data
=
shallowRef
({
foo
: 'bar' })
const {
base64
} = useBase64(
data
, {
serializer
:
v
=>
JSON
.
stringify
(
v
, null, 2),
})

Type Declarations

Show Type Declarations
ts
export interface UseBase64Options {
  /**
   * Output as Data URL format
   *
   * @default true
   */
  
dataUrl
?: boolean
} export interface ToDataURLOptions extends UseBase64Options { /** * MIME type */
type
?: string | undefined
/** * Image quality of jpeg or webp */
quality
?: any
} export interface
UseBase64ObjectOptions
<
T
> extends UseBase64Options {
serializer
?: (
v
:
T
) => string
} export interface UseBase64Return {
base64
:
ShallowRef
<string>
promise
:
ShallowRef
<
Promise
<string>>
execute
: () =>
Promise
<string>
} export declare function
useBase64
(
target
:
MaybeRefOrGetter
<string | undefined>,
options
?: UseBase64Options,
): UseBase64Return export declare function
useBase64
(
target
:
MaybeRefOrGetter
<Blob | undefined>,
options
?: UseBase64Options,
): UseBase64Return export declare function
useBase64
(
target
:
MaybeRefOrGetter
<ArrayBuffer | undefined>,
options
?: UseBase64Options,
): UseBase64Return export declare function
useBase64
(
target
:
MaybeRefOrGetter
<HTMLCanvasElement | undefined>,
options
?: ToDataURLOptions,
): UseBase64Return export declare function
useBase64
(
target
:
MaybeRefOrGetter
<HTMLImageElement | undefined>,
options
?: ToDataURLOptions,
): UseBase64Return export declare function
useBase64
<
T
extends
Record
<string, unknown>>(
target
:
MaybeRefOrGetter
<
T
>,
options
?:
UseBase64ObjectOptions
<
T
>,
): UseBase64Return export declare function
useBase64
<
T
extends
Map
<string, unknown>>(
target
:
MaybeRefOrGetter
<
T
>,
options
?:
UseBase64ObjectOptions
<
T
>,
): UseBase64Return export declare function
useBase64
<
T
extends
Set
<unknown>>(
target
:
MaybeRefOrGetter
<
T
>,
options
?:
UseBase64ObjectOptions
<
T
>,
): UseBase64Return export declare function
useBase64
<
T
>(
target
:
MaybeRefOrGetter
<
T
[]>,
options
?:
UseBase64ObjectOptions
<
T
[]>,
): UseBase64Return

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
IlyaL
Vida Xie
Robin
丶远方
Mikhailov Nikita
wheat
jelf

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.1.0 on
4b4bd - feat: add options for Data URL output control (#4316)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)

Released under the MIT License.

40% Off  yearly access to Vue School, Full course library + Vue.js Master Class.
Claim Offer