---
title: "FileUpload 组件 — zenit Zig UI"
description: "文件选择与拖放入口。Browse、drop-zone、文件列表和应用回调统一成可测试状态。"
url: https://zenit.z.express/zh/components/upload
language: zh-CN
alternate_en: https://zenit.z.express/components/upload.md
alternate_es: https://zenit.z.express/es/components/upload.md
alternate_ja: https://zenit.z.express/ja/components/upload.md
alternate_ko: https://zenit.z.express/ko/components/upload.md
alternate_fr: https://zenit.z.express/fr/components/upload.md
alternate_de: https://zenit.z.express/de/components/upload.md
project: zenit v0.1.0-alpha (Zig 0.15.2, macOS)
source: https://github.com/version-next/zenit
---

# FileUpload

`ui.widgets.FileUpload`

文件选择与拖放入口。

[Video](https://zenit.z.express/media/stories/upload.mp4?v=30b9aa65f9)

**BEHAVIOR CONTRACT**

Browse、drop-zone、文件列表和应用回调统一成可测试状态。

**RECORDED INTERACTION**

Harness 点击 Browse…，Story 回填一个确定性文件路径。

`examples/storybook/stories.zig:3069`

```zig
pub fn buildFileUpload(scope: *ui.Scope, cx: *ui.Cx) anyerror!*ui.Node {
    const a = cx.allocator;
    const c = try col(cx, 12);
    const story = try cx.bindState(FileUploadStory, .{ .state = undefined });
    const fu = try ui.widgets.FileUpload(.{
        .width = 380,
        .on_browse = cx.on(FileUploadStory, story, FileUploadStory.browse),
    }, scope, cx);
    story.state = fu.state;
    fu.wrapper.meta.ownership.meta.test_id = "story.upload.wrapper";
    // 拖放 drop zone：e2e 用 dragAt 往这块矩形里注入 entered/dropped
    fu.state.drop_zone.meta.ownership.meta.test_id = "story.upload.dropzone";
    try c.appendChild(a, fu.wrapper);
    return c;
}
```

[在 GitHub 查看源码examples/storybook/stories.zig:3069](https://github.com/version-next/zenit/blob/HEAD/examples/storybook/stories.zig#L3069)

这是 Storybook 中该 story 的原样代码（zenit 5f9add5+wip 2026-09-30）。其中 `W` 即 `ui.widgets`，`col` / `row` / `label` 是 Storybook 内的布局小工具。

### FileUploadProps

`ui.widgets.FileUpload`

[src/ui/components/file\_upload/mod.zig:26](https://github.com/version-next/zenit/blob/HEAD/src/ui/components/file_upload/mod.zig#L26)

```zig
fn mountFileUpload(props: FileUploadProps, scope: *Scope, cx: *Cx) !FileUploadMount
```

| 字段 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `width` | `f32` | `360` | — |
| `max_files` | `usize` | `8` | — |
| `hint` | `[]const u8` | `"Click Browse to add files"` | — |
| `on_browse` | `?core.HandlerRef` | `null` | Browse 按钮回调（宿主调起对话框后用 state.addFile 回填）。 null = 默认原生 NSOpenPanel（cx.openFilePanel）。 |
| `on_change` | `?core.HandlerRef` | `null` | 文件列表变化回调 |

### FileUploadMount

`ui.widgets.FileUpload`

[src/ui/components/file\_upload/mod.zig:199](https://github.com/version-next/zenit/blob/HEAD/src/ui/components/file_upload/mod.zig#L199)

| 字段 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `wrapper` | `*Node` | — | — |
| `state` | `*FileUploadState` | — | — |
