File Uploads via form actions
You can uploads files similarly to how other forms are handled. In the demo below where you can upload a text or markdown file, with {@attach enhance(...)}, the demo file content you upload is echoed back to you. Without progressive enhancement, the page re-renders and the component reads the result from getRequestContext().form.
With {@attach enhance(...)}
The enhance attachment submits multipart/form-data over fetch and shows the file content inline
Plain HTML
Plain multipart POST, page re-renders with the file content from the form snapshot
<script lang="ts">
import FileUpload from './FileUpload.svelte';
import { files } from './files.ts';
const sources = await loadSources(files);
</script>
<h3>With <code>{'{@attach enhance(...)}'}</code></h3>
<FileUpload label="The enhance attachment submits multipart/form-data over fetch and shows the file content inline" mochi:hydrate />
<h3>Plain HTML</h3>
<FileUpload label="Plain multipart POST, page re-renders with the file content from the form snapshot" />
Styles
<style>
h3 {
margin: 1.5rem 0 0.25rem;
font-size: 0.95rem;
font-weight: 600;
color: var(--text-muted);
}
code {
background: var(--code-bg);
color: var(--code-accent);
padding: 0.05rem 0.35rem;
border-radius: var(--radius-sm);
font-family: var(--font-mono);
font-size: 0.85rem;
}
</style>
More demos
Cookies Read and write cookies on the server and the client through one MochiCookieJar API. File Routes Serve a file from disk with Mochi.file() — static path or a per-request resolver. Crossing the server-client boundary with props How props travel from a server-rendered parent into a hydrated island — Date, Map, Set, BigInt, URL, typed arrays, and even cyclic refs survive devalue’s round-trip.