Learning Tips | Published Nov 17, 2026 | 8 min read

JavaScript FormData: Send Form Fields and Files with Fetch

Learn FormData append and set, repeated fields, file uploads with fetch, multipart boundaries, and safe server-side upload validation.

Diagram traces named form fields and a selected file through FormData and fetch to server-side field and file validation

FormData lets JavaScript collect named form fields and send text and files with fetch. Learn how to construct it from a form, handle repeated names, upload files, avoid multipart boundary mistakes, and validate data on the server.

FormData represents a set of named values using the same multipart format a browser form can submit. It is useful when JavaScript needs to send a file together with text fields without manually assembling the request body.

Create FormData from a form

Give each successful control a name. Constructing FormData from the form collects those named values, including selected files and checked controls.

const form = document.querySelector("#profile-form");

form.addEventListener("submit", async (event) => {
  event.preventDefault();

  const data = new FormData(form);
  data.set("source", "profile-editor");

  try {
    const response = await fetch("/api/profile", {
      method: "POST",
      body: data
    });

    if (!response.ok) {
      throw new Error(`Upload failed: ${response.status}`);
    }

    showSuccess();
  } catch (error) {
    showError(error);
  }
});

FormData includes successful controls: disabled inputs and unchecked checkboxes are not submitted, and controls need names. Validate required fields and file selection before starting an upload, but still validate every value on the server.

Choose append or set

append(name, value) adds another value for a name; set(name, value) replaces existing values for that name. Repeated names are valid, such as multiple selected files. Use getAll() when the server contract expects a list; get() returns only the first associated value.

const data = new FormData();
data.append("tag", "html");
data.append("tag", "css");

console.log(data.getAll("tag")); // ["html", "css"]
data.set("tag", "javascript");
console.log(data.getAll("tag")); // ["javascript"]

Values that are not strings, Blobs, or Files are converted to strings. Convert structured values deliberately; if an API expects JSON, serialize it yourself and agree on the field format with the server.

Send files with fetch

A file input exposes selected files through its files property. Append a File directly to FormData; the browser creates the multipart request body and its boundary.

const fileInput = document.querySelector("#avatar");
const data = new FormData();

data.append("displayName", "Asha");
if (fileInput.files[0]) {
  data.append("avatar", fileInput.files[0]);
}

const response = await fetch("/api/avatar", {
  method: "POST",
  body: data
});

Do not manually set Content-Type: multipart/form-data when sending FormData with fetch. The browser must add the boundary parameter that separates parts of the body. If you set the header yourself without the matching boundary, the server may be unable to parse the form.

Inspect and debug the payload

Use entries() or iteration to inspect field names and values while debugging. A File value may appear as an object rather than readable text. Avoid logging sensitive fields or file contents in production.

  • Check the network panel for the request method, multipart content type with boundary, response status, and server response.
  • Confirm every expected control has a name and is enabled.
  • For repeated fields, confirm the client and server both expect arrays or multiple parts.
  • Check that the file input has an accepted file and that the request is not oversized.

Validate uploads on the server

Client-side checks improve feedback but do not secure an upload. The server should enforce size limits, verify allowed file types using content inspection rather than trusting the filename alone, generate safe storage names, and apply authorization and malware-handling rules appropriate to the application. Treat uploaded filenames and all text values as untrusted input.

FormData handles request encoding; it does not replace API design or server validation. Use it for browser form submissions, let fetch set the multipart boundary, define how repeated names are interpreted, and handle both network failures and non-success HTTP responses.

Want a structured Java learning path?

Explore Jaipur Engineers course options to connect Java fundamentals, debugging, backend development and project practice in a guided learning plan.

Explore the Java program   Ask for course guidance