Skip to content
// EFFECT DAYS 2026

HtmlFiles unstable

Pick files, read a File or Blob with interruption support, and request downloads of files the app creates.

Details

A file input the view renders reports its files in Html.InputPayload. pick opens a picker without one, such as from a menu item or a keyboard shortcut. Either way, send a File on to a Command whose args type it with Schema.instanceOf(File), and read it there with readAsText, readAsBytes or readAsDataUrl. Interrupting that Command, with Command.interrupt, a "switch" run or the component unmounting, cancels the read, so the browser stops reading the file rather than reading it all and dropping the result.

download asks the browser to save a File the app made, such as an export, under the file's name.

Example Previewing a text file, with a cancel button

import { Command, UiSchema } from "effect/ui"
import { HtmlFiles } from "effect/ui/html"
import { Effect, Schema } from "effect"
const Message = UiSchema.TaggedEnum({
ChoseFile: { file: Schema.instanceOf(File) },
ClickedCancel: {},
ReadPreview: { text: Schema.String },
FailedPreview: {}
})
const ReadPreview = Command.make(
"ReadPreview",
{ args: { file: Schema.instanceOf(File) }, mode: "switch" },
({ file }) =>
HtmlFiles.readAsText(file.slice(0, 4096)).pipe(
Effect.match({
onSuccess: (text) => Message.ReadPreview({ text }),
onFailure: () => Message.FailedPreview()
})
)
)
// In `update`: ChoseFile runs `ReadPreview.make({ file })`, and ClickedCancel
// runs `Command.interrupt(ReadPreview)`.
11 exports Since v4.2.0 Source

Commands

pick

unstable
Since v4.2.0

Opens the browser's file picker and sends f's message with the files the user chose, or none when they dismissed it.

When to use

Use to open a picker from something other than a file input, such as a menu item or a keyboard shortcut. A visible "Choose file" control is better as a label for a file input, which the browser opens itself.

Details

See Picked for when the browser refuses to open it, and Files for when it settles.

Signature

declare function pick<Msg>(f: (picked: Picked) => Msg, options: PickOptions): Issued<Msg, never>

Example Opening a picker from a keyboard shortcut

import { UiSchema } from "effect/ui"
import { HtmlFiles } from "effect/ui/html"
import { Schema } from "effect"
const Message = UiSchema.TaggedEnum({
PressedOpen: {},
PickedImages: { files: Schema.Array(Schema.instanceOf(File)) }
})
const command = HtmlFiles.pick(
({ files }) => Message.PickedImages({ files }),
{ accept: ["image/*"], multiple: true }
)

Pick

unstable
Since v4.2.0

The Command behind pick, for a story's .resolve(HtmlFiles.Pick, { opened, files }).

Signature

declare const Pick: Command.Factory<PickOptions, Picked>

Downloading

download

unstable
Since v4.2.0

Requests a download of file under file.name, without confirming a save.

When to use

Use when a Command makes the file, such as one that encodes the Model as a JSON backup, and call it at the end of that Command. Build the file in the Command from plain args, not in update, so a story matches the args by value. The message log still encodes those args, so declare a field that mustn't be logged with Schema.Redacted.

Details

Completion does not tell the app whether the user saved the file, cancelled, or the browser blocked the download. The default service does nothing; BrowserHost.layer also skips the request if object URLs are unavailable. Otherwise, BrowserHost.layer clicks a detached link to an object URL of the file, and revokes the URL 40 seconds later, so interrupting the Command never cancels a download that started.

Signature

declare function download(file: File): Effect<void>

Example Exporting the Model as JSON

import { Command, Component } from "effect/ui"
import { HtmlFiles } from "effect/ui/html"
import { Effect, Schema } from "effect"
const Settings = Schema.Struct({ theme: Schema.String })
const Export = Command.make(
"Export",
{ args: { settings: Settings } },
({ settings }) =>
HtmlFiles.download(
new File([JSON.stringify(settings)], "settings.json", {
type: "application/json"
})
).pipe(Effect.as(Component.noop))
)

Errors

Since v4.2.0

A read of a Blob failed, such as for a file deleted or changed after the user chose it. cause is the browser's error.

Signature

declare class FileReadError extends YieldableError<this> & {
readonly _tag: "FileReadError";
} & Readonly<{
readonly cause: unknown;
}> {
constructor(args: {
readonly cause: unknown;
});
}

Models

Picked

interface unstable
Since v4.2.0

What pick reports: whether the browser opened the picker, and the files the user chose, none when they dismissed it.

Details

The browser refuses to open a picker, giving opened: false, outside a user's action. A Command issued from a click or key press runs within it.

Signature

interface Picked {
readonly files: readonly Array<File>;
readonly opened: boolean;
}

PickOptions

interface unstable
Since v4.2.0

What a file picker shows and allows, as the file input attributes of the same names: the file types it offers, such as ".pdf" or "image/*", whether the user can choose more than one file, and the camera a phone opens to capture a new one.

Signature

interface PickOptions {
readonly accept?: readonly Array<string>;
readonly capture?: "user" | "environment";
readonly multiple?: boolean;
}

Reading

readAsBytes

unstable
Since v4.2.0

Reads all of blob's bytes. Interrupting it cancels the read.

Details

It reads blob.stream(), so it runs wherever Blob does, Node and workers included. Read part of a large file with blob.slice.

Signature

declare function readAsBytes(blob: Blob): Effect<Uint8Array<ArrayBufferLike>, FileReadError>
Since v4.2.0

Reads blob as a data: URL with its type and base64 content, as FileReader.readAsDataURL does. Interrupting it cancels the read.

Details

The URL holds the whole content, so it costs a third more than the file wherever it goes, the Model and the message log included. For a large image, prefer URL.createObjectURL, revoked once the image isn't shown.

Signature

declare function readAsDataUrl(blob: Blob): Effect<string, FileReadError>

readAsText

unstable
Since v4.2.0

Reads blob as text in encoding, UTF-8 by default, as TextDecoder decodes it. Interrupting it cancels the read.

Signature

declare function readAsText(blob: Blob, encoding?: string): Effect<string, FileReadError>

References

Files

unstable
Since v4.2.0

The file picker and downloads. It defaults to one whose picks never settle and whose downloads do nothing.

Signature

declare const Files: Reference<Files>

Services

Files

interface unstable
Since v4.2.0

Opens the browser's file picker, and downloads files.

Details

pick settles when the user chooses files or dismisses the picker. A browser that doesn't report a dismissal leaves it pending, so let the user move on without it. Interrupting it stops waiting, and the picker stays open until the user closes it. BrowserHost.layer provides one that opens a detached file input of its document, and downloads through a detached link. The default's picks never settle, so on a host without a document a pick stays pending, and its downloads do nothing. A test settles picks and sees downloads with TestFiles from effect/ui/html/testing.

Signature

interface Files {
readonly download: (file: File) => Effect<void>;
readonly pick: (options: PickOptions) => Effect<Picked>;
}