createObjectUrl

Category Browser

Creates a reactive URL representing the given object.

Automatically releases the URL when the object changes or the component is unmounted.

Demo

Select file :

Object URL :

none

Usage

NOTE

It uses $effect.

<script lang="ts">
    import { createObjectUrl } from '@sv-use/core';
 
    // Get a file in some way
    // e.g. a dropzone, a cdn, etc.
    const file = $state<File | null>(null);
    const url = createObjectUrl(() => file);
</script>
 
<a href={url.current} download>
    Upload file
</a>

Type Definitions

import type { CleanupFunction, MaybeGetter } from '../__internal__/types.js';
type CreateObjectUrlReturn = {
    readonly current: string | null;
    cleanup: CleanupFunction;
};
/**
 * Creates a reactive URL representing the given object.
 * @param object The object to generate the url for.
 * @see https://svelte-librarian.github.io/sv-use/docs/create-object-url
 */
export declare function createObjectUrl(object: MaybeGetter<Blob | MediaSource | null | undefined>): CreateObjectUrlReturn;
export {};

Sources