File APIs: FormData

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


The FormData object is used to store form input fields values.

It’s especially useful when you need to send files to the server.

It’s probably the only time you’ll actually need it.

Here is one example of using FormData to send the content of a file using fetch.

We have an input field:

<input type="file" id="fileUpload" />

We attach a change event handler on it:

document.querySelector('#fileUpload').addEventListener('change', (event) => {
  handleImageUpload(event)
})

and we manage the bulk of our logic in the handleImageUpload() function:

const handleImageUpload = (event) => {
  const files = event.target.files
  const formData = new FormData()
  formData.append('myFile', files[0])

  fetch('/saveImage', {
    method: 'POST',
    body: formData,
  })
    .then((response) => response.json())
    .then((data) => {
      console.log(data)
    })
    .catch((error) => {
      console.error(error)
    })
}

In this example we POST to the /saveImage endpoint.

You could send more data too by appending it to the formData object.

In the server-side, to access the file data you must parse the request as a multipart form.

See for example how to upload files in a Next.js form

The FormData object you create has many useful methods:

  • append() to add a value to the object, with the specified key. If the key already exists, the value is added to that key, without eliminating the first one
  • delete() deletes a key value pair
  • entries() gives an Iterator object you can loop to list the key value pairs hosted
  • get() get the value associated with a key. If more than one value was appended, it returns the first one
  • getAll() get all the values associated with a key
  • has() returns true if there’s a key
  • keys() gives an Iterator object you can loop to list the keys hosted
  • set() to add a value to the object, with the specified key. If the key already exists, the value is replaced
  • values() gives an Iterator object you can loop to list the values hosted

The FormData object is part of the XMLHttpRequest 2 spec.

It’s available in all the modern browsers and it’s most often used when sending images through fetch.

See how to handle images uploaded server-side

Lessons in this unit:

0: Introduction
1: The File object
2: FileList
3: FileReader
4: Blob
5: ▶︎ FormData
6: Accept only images in file input
7: Check if checkbox is checked
8: Reset a form
9: File upload with server handling
10: Drag and drop file upload
11: Validating file size before upload