--- includeMockServer: true title: "File Upload" description: Upload files with progress and validation icon: "icon-[mdi--file-upload-outline]" soon: true ---
## Basic usage On the client, set `hx-encoding` to `multipart/form-data`: ```html
``` - [`hx-encoding`](/reference/attributes/hx-encoding)=`"multipart/form-data"` sends the form as [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData), required for file uploads. - [`hx-post`](/reference/attributes/hx-post) submits the form to `/upload`. On the server, respond with a success or error message: ```html

File uploaded successfully.

``` ## Preserving file selection on errors When a form re-renders with validation errors, file inputs lose their selection. Add [`hx-preserve`](/reference/attributes/hx-preserve) to keep it: ```html
``` Try it in the demo: select a file, then submit with empty fields. The form re-renders with errors, but your file selection stays. Alternatively, place the file input outside the swap target using the [`form` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form): ```html
``` The input is outside the form element, so it is never replaced during swaps. ## Upload progress htmx 4.x uses the native [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) API. `fetch` supports [upload progress monitoring](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#monitoring_upload_progress) in some browsers, but cross-browser support is limited. For reliable progress tracking, use [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/upload) directly: ```html
```