How to Upload Files to a Form Backend

Learn how file uploads work with HTML forms and JavaScript FormData, including multipart/form-data and best practices.

File uploads require a different encoding than normal form submissions. The key is: multipart/form-data.

In this guide you’ll learn how to upload files using:

  • Plain HTML form submission
  • JavaScript + FormData

1) HTML file upload form

Use method="post" and enctype="multipart/form-data".

2) Restrict allowed file types

Use the accept attribute to hint which file types are allowed.

3) Multiple file uploads

Add multiple to allow selecting multiple files.

4) Upload with JavaScript (FormData)

For custom UX (progress, previews, async), use fetch with FormData.

5) Best practices

  • Validate file size and type on the backend.
  • Limit max upload size to prevent abuse.
  • Use antivirus scanning for untrusted uploads (if needed for your use case).
  • Store files securely and avoid exposing raw storage URLs publicly.

Related guides