File Uploads
HTML Living Standard
A file picker lets a user select a file. To upload it, put the picker in a form that sends the selected file to a server that accepts it.
On this page
Submit a File with multipart/form-data
action="/upload" points to the server route. method="post" sends the form in the request body. enctype="multipart/form-data" tells the browser to send each field as a separate part, including the selected file’s contents. The input’s name="attachment" gives the file part a name the server can look up.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Submit a File with multipart/form-data</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="attachment">Choose a text file</label>
<input id="attachment" name="attachment" type="file" accept=".txt,text/plain" required>
<button type="submit">Upload file</button>
</form>
</body>
</html>
What to expect Choose a small text file and click Upload file. The preview shows the method, destination, encoding, filename, and size using the file selected in the browser. Follow the next example to send the actual file to your local server.
Receive the File on a Local Server
Save the previous HTML example as upload.html. Save the server code below as server.mjs in the same folder. Run node server.mjs with Node.js 22 or later, then open http://127.0.0.1:3000. Choose a nonempty .txt file and submit. The server reads the attachment part and returns its filename and byte count. This local demo limits the whole request to 1 MiB and discards the file after reading it.
import { readFile } from "node:fs/promises";
import { createServer } from "node:http";
const page = await readFile(new URL("./upload.html", import.meta.url));
const maxRequestBytes = 1024 * 1024;
function send(response, status, body) {
response.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
response.end(JSON.stringify(body));
}
createServer(async (request, response) => {
const path = request.url;
if (request.method === "GET" && path === "/") {
response.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
response.end(page);
return;
}
if (request.method !== "POST" || path !== "/upload") {
send(response, 404, { error: "Page not found" });
return;
}
const contentType = request.headers["content-type"] ?? "";
if (!contentType.toLowerCase().startsWith("multipart/form-data;")) {
send(response, 415, { error: "Send a multipart form" });
return;
}
try {
const chunks = [];
let size = 0;
for await (const chunk of request.iterator({ destroyOnReturn: false })) {
size += chunk.length;
if (size > maxRequestBytes) {
response.setHeader("Connection", "close");
send(response, 413, { error: "Request exceeds 1 MiB" });
return;
}
chunks.push(chunk);
}
const multipart = new Response(Buffer.concat(chunks), {
headers: { "Content-Type": contentType },
});
const form = await multipart.formData();
const file = form.get("attachment");
if (!(file instanceof File) || !file.name || file.size === 0) {
send(response, 400, { error: "Choose a nonempty file" });
return;
}
send(response, 200, { filename: file.name, bytes: file.size });
} catch {
send(response, 400, { error: "Could not read the upload" });
}
}).listen(3000, "127.0.0.1", () => {
console.log("Open http://127.0.0.1:3000");
});
What to expect A successful upload shows JSON such as {"filename":"notes.txt","bytes":12}. The byte count depends on your file.