# File upload

SvFileUpload: a drag-and-drop file field with click-to-browse, accept / maxSize / maxFiles validation (rejects fire onReject) and a selected-files list. Carries the shared editor contract - label, hint, required/error validation - so it drops into a form like any input.

Uses `@svgrid/grid` (MIT). Category: Inputs. Live demo: https://svgrid.com/demos/287-file-upload/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvFileUpload in Svelte 5: a drag-and-drop file field with click to browse, accept, maxSize and maxFiles validation whose rejections fire onReject, and a selected-files list. It carries the shared editor contract with a label, a hint and required or error validation, so it drops into a form like any other input; the demo uses it for a required avatar limited to images.

SvFileUpload - a drag-and-drop file field with click-to-browse, accept / size / count validation and a selected-files list. Carries the shared editor contract (label, hint, required/error validation), so it drops into a form exactly like the other inputs.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### How are rejected files reported?

Files that fail accept, maxSize or maxFiles are not added to the value; onReject receives them with a reason so you can show a message, as the demo does under the field.

### Does it upload the files?

No. It collects File objects through onChange; you send them with fetch or FormData when the form submits, which keeps the component free of network assumptions.

### Can users remove a selected file?

Yes. Each entry in the list has a remove button, and the value updates through onChange.

## Related documentation

- [SvFileUpload](https://svgrid.com/docs/help/ui-components/sv-file-upload.md)

## Related articles

- [SvGrid UI Components Tips and Tricks: Svelte 5 Buttons, Inputs, Tree, Forms](https://svgrid.com/blog/svgrid-ui-components-tips-and-tricks/) - Practical tips for the SvGrid UI component kit - SvButton states, a Cmd+K command palette, a virtualized searchable tree, schema-driven forms, a parsing date/time field, and the shared field contract - each with a code snippet.

## Source code (287-file-upload.svelte)

```svelte
<script lang="ts">
  /**
   * SvFileUpload - a drag-and-drop file field with click-to-browse, accept /
   * size / count validation and a selected-files list. Carries the shared editor
   * contract (label, hint, required/error validation), so it drops into a form
   * exactly like the other inputs.
   */
  import { SvFileUpload } from '@svgrid/grid'
  import type { FileRejection } from '@svgrid/grid'

  let avatar = $state<File[]>([])
  let docs = $state<File[]>([])
  let lastReject = $state<string>('')

  const onReject = (rejections: FileRejection[]) => {
    const r = rejections[0]
    lastReject = r ? `Rejected ${r.file.name} (${r.reason})` : ''
  }
  // A required single-file field is invalid until a file is chosen.
  const avatarError = $derived(avatar.length ? undefined : 'An avatar image is required')
</script>

<div class="wrap">
  <header>
    <h2>File upload</h2>
    <p>Drag files onto the zone or click to browse. <code>accept</code>, <code>maxSize</code> and <code>maxFiles</code> validate on drop; rejects fire <code>onReject</code>. Same label / validation contract as every field.</p>
  </header>

  <div class="cols">
    <section>
      <h3>Single (required, images only)</h3>
      <SvFileUpload
        files={avatar}
        label="Avatar"
        accept="image/*"
        required
        invalid={!!avatarError}
        error={avatarError}
        onChange={(f) => (avatar = f)}
        onReject={onReject}
      />
    </section>

    <section>
      <h3>Multiple (PDF, max 3, 5 MB each)</h3>
      <SvFileUpload
        files={docs}
        label="Documents"
        hint="Up to 3 PDFs, 5 MB each"
        accept=".pdf"
        multiple
        maxFiles={3}
        maxSize={5 * 1024 * 1024}
        onChange={(f) => (docs = f)}
        onReject={onReject}
      />
      {#if lastReject}<p class="reject">{lastReject}</p>{/if}
    </section>
  </div>
</div>

<style>
  .wrap { padding: 20px; max-width: 820px; display: flex; flex-direction: column; gap: 16px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; max-width: 660px; }
  code { background: var(--sg-row-hover-bg, #eef2ff); padding: 1px 5px; border-radius: 5px; font-size: 12px; }
  .cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 24px; align-items: start; }
  section h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--sg-muted, #64748b); }
  .reject { margin: 10px 0 0; font-size: 12.5px; color: var(--sg-danger, #dc2626); font-weight: 600; }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/287-file-upload.svelte)
