Skip to content

Object Url

URL representing an object.

๐ŸŽฌ Usage

<script>
    import {object_url} from "@sveu/shared"

    let file = undefined

    const on_file_change = (e) => {
        const target = e.target
        const files = target.files
        file = files && files.length > 0 ? files[0] : undefined
    }

    $: url = object_url(file)
</script>

<input type="file" on:change="{on_file_change}" />

{url ? url : ""}

๐Ÿ‘ฉโ€๐Ÿ’ปAPI

Arguments

Name Description Type Required
value The value to create an object URL for. Blob | MediaSource Yes

Returns

A URL representing the object.

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
import { on_destroy } from "../on_destroy"

/**
 * URL representing an object.
 *
 * @param value - The value to create an object URL for.
 *
 * @returns The object URL.
 */
export function object_url(value: Blob | MediaSource | undefined) {
    let url: string | undefined = ""

    /** Clean up the object URL. */
    function release() {
        if (url) URL.revokeObjectURL(url)

        url = undefined
    }

    release()

    if (value) url = URL.createObjectURL(value)

    on_destroy(release)

    return url
}

Last update: 2023-02-18