Skip to content

useObjectUrl

Category
Export Size
247 B
Last Changed
8 months ago

Reactive URL representing an object.

Creates an URL for the provided File, Blob, or MediaSource via URL.createObjectURL() and automatically releases the URL via URL.revokeObjectURL() when the source changes or the component is unmounted.

Demo

Select file:
Object URL:
none

Usage

vue
<script setup lang="ts">
import { 
useObjectUrl
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
file
=
shallowRef
()
const
url
=
useObjectUrl
(
file
)
function
onFileChange
(
event
) {
file
.
value
=
event
.target.files[0]
} </script> <template> <
input
type
="file" @
change
="
onFileChange
">
<
a
:href
="
url
">Open file</
a
>
</template>

Component Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

vue
<template>
  <UseObjectUrl v-slot="
url
"
:object
="file">
<
a
:href
="
url
">Open file</
a
>
</UseObjectUrl> </template>

Type Declarations

ts
/**
 * Reactive URL representing an object.
 *
 * @see https://vueuse.org/useObjectUrl
 * @param object
 */
export declare function 
useObjectUrl
(
object
:
MaybeRefOrGetter
<Blob | MediaSource | null | undefined>,
):
Readonly
<
Ref
<string | undefined, string | undefined>>

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
Anthony Fu
IlyaL
SerKo
Andrew Ferreira
vaakian X
BaboonKing
Michael Hüneburg

Changelog

v14.0.0-alpha.0 on
8c521 - feat(components)!: refactor components and make them consistent (#4912)
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.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.3.0 on
d3f7e - fix: improve parameter type (#3239)

Released under the MIT License.

Build smarter with AI
Join a free workshop to explore LLMs and advanced AI tools.
Register now
October 22