> ## Documentation Index
> Fetch the complete documentation index at: https://prefab.prefect.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Open File Picker

> Open the browser file picker from any clickable element.

export const ComponentPreview = ({json, height, resizable, bare, hideJson, playground, children}) => {
  const hostRef = React.useRef(null);
  const handleRef = React.useRef(null);
  const cardRef = React.useRef(null);
  const containerRef = React.useRef(null);
  const [previewWidth, setPreviewWidth] = React.useState(null);
  const [isDragging, setIsDragging] = React.useState(false);
  const [playgroundHref, setPlaygroundHref] = React.useState(null);
  React.useEffect(function () {
    if (!playground) return;
    var isLocal = location.hostname === "localhost" || location.hostname === "127.0.0.1";
    var path = isLocal ? "/playground" : "/docs/playground";
    var url = new URL(path, window.location.href);
    url.hash = "code=" + playground;
    setPlaygroundHref(url.href);
  }, [playground]);
  var jsonStr = typeof json === "string" ? json : JSON.stringify(json, null, 2);
  React.useEffect(function () {
    if (cardRef.current) {
      var card = cardRef.current.closest(".group");
      if (card) card.classList.remove("group");
    }
    var host = hostRef.current;
    if (!host || !json) return;
    function mount() {
      if (!window.__prefab || !host) return;
      var dark = document.documentElement.classList.contains("dark");
      handleRef.current = window.__prefab.mountPreview(host, jsonStr, {
        dark: dark
      });
      if (bare && host.shadowRoot) {
        var m = host.shadowRoot.querySelector("[data-prefab-mount]");
        if (m) m.style.background = "transparent";
      }
    }
    if (window.__prefab) {
      mount();
    } else {
      if (!window.__prefabLoading) {
        if (!window.__prefabReady) {
          var s = document.createElement("script");
          s.src = "/renderer.js";
          document.head.appendChild(s);
        }
        window.__prefabLoading = new Promise(function (resolve) {
          function check() {
            if (window.__prefabReady) {
              window.__prefabReady.then(resolve);
            } else {
              setTimeout(check, 10);
            }
          }
          check();
        });
      }
      window.__prefabLoading.then(mount);
    }
    var observer = new MutationObserver(function () {
      var dark = document.documentElement.classList.contains("dark");
      if (handleRef.current) handleRef.current.setDark(dark);
    });
    observer.observe(document.documentElement, {
      attributes: true,
      attributeFilter: ["class"]
    });
    return function () {
      observer.disconnect();
      if (handleRef.current) {
        handleRef.current.unmount();
        handleRef.current = null;
      }
    };
  }, [json]);
  function startResize(e) {
    e.preventDefault();
    var startX = e.clientX;
    var startW = hostRef.current ? hostRef.current.offsetWidth : 0;
    var maxW = containerRef.current ? containerRef.current.offsetWidth - 10 : startW;
    setIsDragging(true);
    function onMove(ev) {
      var w = Math.max(260, Math.min(startW + (ev.clientX - startX), maxW));
      setPreviewWidth(w);
    }
    function onUp() {
      document.removeEventListener("mousemove", onMove);
      document.removeEventListener("mouseup", onUp);
      setIsDragging(false);
    }
    document.addEventListener("mousemove", onMove);
    document.addEventListener("mouseup", onUp);
  }
  if (bare) {
    return <div ref={hostRef} />;
  }
  if (resizable) {
    return <Card>
        <div ref={cardRef} />
        <div ref={containerRef} style={{
      display: "flex",
      position: "relative",
      overflow: "hidden"
    }}>
          <div ref={hostRef} style={{
      flex: previewWidth ? "none" : 1,
      width: previewWidth ? previewWidth + "px" : undefined,
      minWidth: 0
    }} />
          <div onMouseDown={startResize} style={{
      width: "14px",
      flexShrink: 0,
      cursor: "col-resize",
      background: isDragging ? "var(--border, #e5e7eb)" : "color-mix(in srgb, var(--border, #e5e7eb) 40%, transparent)",
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      transition: "background 0.15s",
      userSelect: "none"
    }}>
            <svg width="6" height="24" style={{
      opacity: 0.5
    }}>
              <circle cx="2" cy="4" r="1.2" fill="currentColor" />
              <circle cx="2" cy="9" r="1.2" fill="currentColor" />
              <circle cx="2" cy="14" r="1.2" fill="currentColor" />
              <circle cx="2" cy="19" r="1.2" fill="currentColor" />
              <circle cx="5" cy="4" r="1.2" fill="currentColor" />
              <circle cx="5" cy="9" r="1.2" fill="currentColor" />
              <circle cx="5" cy="14" r="1.2" fill="currentColor" />
              <circle cx="5" cy="19" r="1.2" fill="currentColor" />
            </svg>
          </div>
          <div style={{
      flex: previewWidth ? 1 : "0 0 24px",
      minWidth: "24px",
      background: "repeating-linear-gradient(-45deg, transparent, transparent 3px, var(--border, #e5e7eb) 3px, var(--border, #e5e7eb) 4px)",
      opacity: 0.4
    }} />
          {previewWidth && <div style={{
      position: "absolute",
      bottom: "8px",
      right: "8px",
      background: "var(--background, white)",
      border: "1px solid var(--border, #e5e7eb)",
      borderRadius: "4px",
      padding: "1px 6px",
      fontSize: "11px",
      fontFamily: "monospace",
      color: "var(--muted-foreground, #6b7280)",
      pointerEvents: "none"
    }}>
              {previewWidth}px
            </div>}
        </div>
        {playgroundHref && <div style={{
      display: "flex",
      justifyContent: "flex-end",
      padding: "4px 0 8px"
    }}>
            <a href={playgroundHref} target="_blank" rel="noopener noreferrer" style={{
      fontSize: "11px",
      opacity: 0.4,
      textDecoration: "none",
      textDecorationLine: "none",
      borderBottom: "none",
      boxShadow: "none",
      display: "inline-flex",
      alignItems: "center",
      gap: "3px",
      transition: "opacity 0.15s",
      fontWeight: 400
    }} onMouseOver={function (e) {
      e.currentTarget.style.opacity = "0.6";
    }} onMouseOut={function (e) {
      e.currentTarget.style.opacity = "0.4";
    }}>
              Edit in Playground
              <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 3h6v6" /><path d="M10 14 21 3" /><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /></svg>
            </a>
          </div>}
        <div style={{
      marginBottom: "-2rem"
    }}>{children}</div>
      </Card>;
  }
  return <Card>
      <div ref={cardRef} />
      <div ref={hostRef} />
      {playgroundHref && <div style={{
    display: "flex",
    justifyContent: "flex-end",
    padding: "4px 0 8px"
  }}>
          <a href={playgroundHref} target="_blank" rel="noopener noreferrer" style={{
    fontSize: "11px",
    opacity: 0.4,
    textDecoration: "none",
    textDecorationLine: "none",
    borderBottom: "none",
    boxShadow: "none",
    display: "inline-flex",
    alignItems: "center",
    gap: "3px",
    transition: "opacity 0.15s",
    fontWeight: 400
  }} onMouseOver={function (e) {
    e.currentTarget.style.opacity = "0.6";
  }} onMouseOut={function (e) {
    e.currentTarget.style.opacity = "0.4";
  }}>
            Edit in Playground
            <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 3h6v6" /><path d="M10 14 21 3" /><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /></svg>
          </a>
        </div>}
      <div style={{
    marginBottom: "-2rem"
  }}>{children}</div>
    </Card>;
};

`OpenFilePicker` is an action, not a component. It opens the browser's native file picker when triggered — attach it to any clickable element and it handles the rest: reading selected files to base64 and passing them to `onSuccess` as `$event`.

This is the counterpart to [DropZone](/components/drop-zone). Where DropZone gives you a dedicated upload area, OpenFilePicker turns any existing element into a file input. A button, a badge, an icon — anything with an `on_click`.

<Info>
  All file reading happens in the browser. Files are never sent to a server unless you explicitly wire up an `on_success` action (like `CallTool`) to do so.
</Info>

<ComponentPreview json={{"view":{"type":"Button","label":"Upload File","variant":"default","size":"default","disabled":false,"onClick":{"action":"openFilePicker","multiple":false}}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQnV0dG9uCmZyb20gcHJlZmFiX3VpLmFjdGlvbnMgaW1wb3J0IE9wZW5GaWxlUGlja2VyCgpCdXR0b24oIlVwbG9hZCBGaWxlIiwgb25fY2xpY2s9T3BlbkZpbGVQaWNrZXIoKSkK">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Button
    from prefab_ui.actions import OpenFilePicker

    Button("Upload File", on_click=OpenFilePicker())
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "type": "Button",
        "label": "Upload File",
        "variant": "default",
        "size": "default",
        "disabled": false,
        "onClick": {"action": "openFilePicker", "multiple": false}
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

## Processing Files with a Tool

Files are read client-side, but they don't go anywhere until you tell them to. The typical pattern is to nest a `CallTool` inside `on_success` — pick the file, then send its base64 data to your MCP server for processing.

<ComponentPreview json={{"view":{"type":"Button","label":"Upload CSV","icon":"upload","variant":"outline","size":"default","disabled":false,"onClick":{"onSuccess":{"action":"toolCall","tool":"process_csv","arguments":{"file":"{{ $event }}"}},"action":"openFilePicker","accept":".csv","multiple":false}}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQnV0dG9uCmZyb20gcHJlZmFiX3VpLmFjdGlvbnMgaW1wb3J0IE9wZW5GaWxlUGlja2VyCmZyb20gcHJlZmFiX3VpLmFjdGlvbnMubWNwIGltcG9ydCBDYWxsVG9vbAoKQnV0dG9uKAogICAgIlVwbG9hZCBDU1YiLAogICAgaWNvbj0idXBsb2FkIiwKICAgIHZhcmlhbnQ9Im91dGxpbmUiLAogICAgb25fY2xpY2s9T3BlbkZpbGVQaWNrZXIoCiAgICAgICAgYWNjZXB0PSIuY3N2IiwKICAgICAgICBvbl9zdWNjZXNzPUNhbGxUb29sKAogICAgICAgICAgICAicHJvY2Vzc19jc3YiLAogICAgICAgICAgICBhcmd1bWVudHM9eyJmaWxlIjogInt7ICRldmVudCB9fSJ9LAogICAgICAgICksCiAgICApLAopCg">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Button
    from prefab_ui.actions import OpenFilePicker
    from prefab_ui.actions.mcp import CallTool

    Button(
        "Upload CSV",
        icon="upload",
        variant="outline",
        on_click=OpenFilePicker(
            accept=".csv",
            on_success=CallTool(
                "process_csv",
                arguments={"file": "{{ $event }}"},
            ),
        ),
    )
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "type": "Button",
        "label": "Upload CSV",
        "icon": "upload",
        "variant": "outline",
        "size": "default",
        "disabled": false,
        "onClick": {
          "onSuccess": {
            "action": "toolCall",
            "tool": "process_csv",
            "arguments": {"file": "{{ $event }}"}
          },
          "action": "openFilePicker",
          "accept": ".csv",
          "multiple": false
        }
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

## Multiple Files

Set `multiple=True` to let users select several files at once. `$event` is always `list[FileUpload]` regardless of `multiple`, but the OS file picker restricts multi-select to when `multiple=True`.

```python Multiple file upload theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
from prefab_ui.components import Button
from prefab_ui.actions import OpenFilePicker
from prefab_ui.actions.mcp import CallTool

Button(
    "Upload Images",
    on_click=OpenFilePicker(
        accept="image/*",
        multiple=True,
        on_success=CallTool(
            "process_images",
            arguments={"images": "{{ $event }}"},
        ),
    ),
)
```

## File Size Limits

The `max_size` parameter rejects files that exceed a byte limit before they ever reach `onSuccess`. Oversized files are silently dropped with a toast notification — the user sees the error, your tool never has to deal with it.

```python With size limit theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
from prefab_ui.components import Button
from prefab_ui.actions import OpenFilePicker
from prefab_ui.actions.mcp import CallTool

Button(
    "Upload (max 5MB)",
    on_click=OpenFilePicker(
        max_size=5_000_000,
        on_success=CallTool("upload", arguments={"file": "{{ $event }}"}),
    ),
)
```

## Error Handling

The `on_error` callback on the nested `CallTool` fires if the server-side processing fails. The error message is available as `$error`.

```python With error handling theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
from prefab_ui.components import Button
from prefab_ui.actions import OpenFilePicker, ShowToast
from prefab_ui.actions.mcp import CallTool

Button(
    "Upload",
    on_click=OpenFilePicker(
        accept=".csv",
        on_success=CallTool(
            "process_csv",
            arguments={"file": "{{ $event }}"},
            on_success=ShowToast("File processed!", variant="success"),
            on_error=ShowToast(
                "Processing failed: {{ $error }}",
                variant="error",
            ),
        ),
    ),
)
```

## The `FileUpload` Type

Both OpenFilePicker and [DropZone](/components/drop-zone) produce the same `$event` shape. Prefab ships a `FileUpload` type you can use to annotate your tool parameters — see [The FileUpload Type](/components/drop-zone#the-fileupload-type) for details and usage.

## User Activation

One constraint worth knowing: `OpenFilePicker` must execute before any async server actions in the same chain. Browsers require a recent user gesture to open the file picker — if a `CallTool` or `SendMessage` fires first, the activation window expires and the picker silently won't open. This is the natural ordering anyway (pick the file, then send it), but it's worth understanding why.

## API Reference

<Card icon="code" title="OpenFilePicker Parameters">
  <ParamField body="accept" type="str | None" default="None">
    File type filter. Accepts MIME types (`"image/*"`), extensions (`".csv"`), or a comma-separated mix.
  </ParamField>

  <ParamField body="multiple" type="bool" default="False">
    Allow selecting multiple files. `$event` is always `list[FileUpload]`; this controls whether the OS file picker allows multi-select.
  </ParamField>

  <ParamField body="max_size" type="int | None" default="None">
    Maximum file size in bytes. Oversized files are rejected with a toast notification.
  </ParamField>

  <ParamField body="on_success" type="Action | list[Action] | None" default="None">
    Action(s) to run after files are selected. `$event` is always `list[FileUpload]`.
  </ParamField>

  <ParamField body="on_error" type="Action | list[Action] | None" default="None">
    Action(s) to run if file reading fails.
  </ParamField>
</Card>

## Protocol Reference

```json OpenFilePicker theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
{
  "action": "openFilePicker",
  "accept?": "string",
  "multiple?": false,
  "maxSize?": "number"
}
```

For the complete protocol schema, see [OpenFilePicker](/protocol/open-file-picker).
