| CARVIEW |
Select Language
HTTP/2 301
cache-status: "Netlify Edge"; fwd=miss
content-type: text/html
date: Fri, 26 Dec 2025 19:57:19 GMT
location: /examples/file-upload/
server: Netlify
strict-transport-security: max-age=31536000
x-nf-request-id: 01KDE3SBSEX0AZ6ZMHXR3XRNCX
content-length: 98
HTTP/2 200
accept-ranges: bytes
age: 2
cache-control: public,max-age=0,must-revalidate
cache-status: "Netlify Edge"; fwd=miss
content-encoding: gzip
content-type: text/html; charset=UTF-8
date: Fri, 26 Dec 2025 19:57:20 GMT
etag: "82fefe79148baa08abbcc8762bce4bd3-ssl-df"
server: Netlify
strict-transport-security: max-age=31536000
vary: Accept-Encoding
x-nf-request-id: 01KDE3SC1JS08S0K91TTM0XFZ0
</> htmx ~ Examples ~ File Upload
File Upload
In this example we show how to create a file upload form that will be submitted via ajax, along with a progress bar.
We will show two different implementation, one in pure javascript (using some utility methods in htmx) and one in hyperscript
First the pure javascript version.
- We have a form of type
multipart/form-dataso that the file will be properly encoded - We post the form to
/upload - We have a
progresselement - We listen for the
htmx:xhr:progressevent and update thevalueattribute of the progress bar based on theloadedandtotalproperties in the event detail.
<form id='form' hx-encoding='multipart/form-data' hx-post='/upload'>
<input type='file' name='file'>
<button>
Upload
</button>
<progress id='progress' value='0' max='100'></progress>
</form>
<script>
htmx.on('#form', 'htmx:xhr:progress', function(evt) {
htmx.find('#progress').setAttribute('value', evt.detail.loaded/evt.detail.total * 100)
});
</script>
The Hyperscript version is very similar, except:
- The script is embedded directly on the form element
- Hyperscript offers nicer syntax (although the htmx API is pretty nice too!)
<form hx-encoding='multipart/form-data' hx-post='/upload'
_='on htmx:xhr:progress(loaded, total) set #progress.value to (loaded/total)*100'>
<input type='file' name='file'>
<button>
Upload
</button>
<progress id='progress' value='0' max='100'></progress>
</form>
Note that hyperscript allows you to destructure properties from details directly into variables