DEV BOX

'26/12/04 UPDATE

WEB / FILE UPLOAD / FIELD NOTES

I picked a file and submitted the form. Why did the server get only its name?

A form without enctype sends only the file name as a string. With Chromium 154 this note compares forms and fetch against a server log, shows why setting Content-Type by hand breaks parsing, and tests multer's garbled file names and the names, types and sizes a server must not trust.

You submit a form with a file input and the server log shows only file=notes.txt, or your upload library says it found no file. The request usually deserves a look before the server code. By the end of this note you can tell the cases apart from the request's Content-Type line, fix forms and fetch correctly, and check the values an upload server must not trust.

The examples ran on 2026-11-02 on macOS 27.0.1. The browser was Chromium 154.0.8037.100 (the build inside the Aside app, --headless=new, empty profile). Files were chosen with DOM.setFileInputFiles from the DevTools Protocol, not by hand in a file dialog. The servers were Node.js 24.21.0 node:http with the standard Response.formData() parser, plus Express 5.3.0 and multer 2.5.0 (busboy 1.6.0) installed in a temporary folder. curl was 8.7.1. The whole set ran twice and the output matched except for the time on its first line. Safari, Firefox and servers in other languages were not run. All file names are fictional.

Without enctype, only the file name goes

The same notes.txt (13 bytes) was chosen in four forms. In form A, with no enctype, the server received application/x-www-form-urlencoded and file was not a file but the string notes.txt. No error appeared anywhere.

forms.mjs output. A is urlencoded with file as the string notes.txt. B is multipart with File notes.txt 13 bytes. C has file=notes.txt in the URL and a 0-byte body. D lists only title in its field names.
The same notes.txt was chosen in all four forms. Compare the file lines: the file's content reached the server only in B, while A received the string notes.txt instead of the 13-byte file.

This follows the standard. In the HTML Standard the default enctype is urlencoded, and the step that turns entries into name and value pairs says that when a value is a File, its name becomes the value. Form C, with method="get", put the file name in the URL even though enctype was set. MDN also describes enctype as a setting for when method is post. In form D, whose input has no name, the file was left out of the request entirely. The fix is two attributes.

<form id="b" action="/echo" method="post" enctype="multipart/form-data">
  <input name="title" value="hello">
  <input name="file" type="file">
  <button>Send</button>
</form>

With fetch, the fix is to not write Content-Type

When fetch gets a FormData, the browser picks a boundary string and puts it in the header. MDN says not to set Content-Type yourself here, because that stops the browser from adding the boundary. Cases E and F show exactly this difference.

fetch(u, { method: 'POST', body: new FormData(form) }).then(answer);

// F: the same call with this one line added fails on the server
headers: { 'Content-Type': 'multipart/form-data' },
fetch output of forms.mjs and an empty form submit. E has bound yes and File notes.txt 13 bytes. F has bound no and TypeError, Failed to parse body as FormData. G body is {"file":{}}. H body is title=hello&file=%5Bobject+File%5D. I shows File (empty name) 0 bytes.
E and F differ by one headers line. The bound line says whether Content-Type has a boundary. F shows no, so the server's parser could not split the body.

In F the Node.js parser failed with Failed to parse body as FormData. Putting a File in JSON.stringify, as in G, sent only {"file":{}}. Converting to URLSearchParams, as in H, sent the text [object File]. Neither sent the file's content. The last case, I, is a different trap. A multipart form submitted with nothing chosen still sent a file part with an empty name and 0 bytes. The standard asks for this, so with a parser that passes this part along, as the Node.js one does, a server that only checks whether the file field exists misses empty uploads. Check the size too.

How it looks in multer

The same form and code went to Express with multer. As the multer README says it does not process forms that are not multipart, in A req.file was empty and only the string remained in the body read by express.urlencoded. F gave Multipart: Boundary not found, returned as a 400 by the lab's error handler. In case I, with nothing chosen, multer 2.5.0, unlike the Node.js parser, dropped the part with the empty file name, so req.file was empty. So when you see these two symptoms, look at the request before the multer settings.

Even with a correct request, the name can come out garbled. résumé.txt became résumé.txt with default settings, and a Korean file name broke the same way. The README gives the default of defParamCharset as latin1, while Chromium sent the name as UTF-8 in these runs. With defParamCharset: 'utf8' both names came through intact. RFC 7578 also says file names are typically UTF-8, but adds that HTML forms use the form's charset, so check first on a page that is not UTF-8.

drive.mjs output. A has file null and notes.txt as a string in body. F gives Multipart: Boundary not found. B has name notes.txt, 13 bytes. The name is résumé.txt on /basic and résumé.txt on /safe.
A and F showed the same symptoms with a different server package. On the name lines, /basic garbled résumé.txt into résumé.txt, and /safe, with the setting added, kept it intact.

Name, type and size are set by the sender

Once files arrive, the risk runs the other way. curl can send values a browser would not. With filename=../../evil.sh, the Node.js File.name stayed ../../evil.sh. A multer route without preservePath cut it to evil.sh, but a route using preservePath: true and saving to path.join(folder, originalname) really did create a file two levels above the upload folder. This was reproduced on purpose inside a temporary folder, so do not use that code. The multer README also says to treat originalname as untrusted.

curl-cases.sh output. /safe gives originalname evil.sh and a stored name of 32 hex characters. /unsafe-save gives wroteTo ../../evil.sh and outside upload folder: 1. evil.sh declared image/png on /safe gives mimetype image/png. /image-only says not a PNG file. /basic accepted 2 MB and /safe gives LIMIT_FILE_SIZE.
These requests came from curl. In the /unsafe-save group, outside upload folder: 1 means evil.sh really appeared two levels above the upload folder, while the /safe group stored the file under a random name.

A text file declared with type=image/png came through with mimetype set to image/png. A route with no size limit accepted 2 MB, and the README lists no limit by default for this setting. A file name that imitates an HTML tag with angle brackets also came through unchanged with the settings below. These are the settings that ran: let dest pick a random stored name, use the original name only for display and escape it when you put it in a page, limit the size, and read the first bytes yourself.

const safe = multer({
  dest: folder,
  defParamCharset: 'utf8',
  limits: { fileSize: 1024 * 1024, files: 1 },
});

const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
const isPng = async file => {
  const handle = await fs.promises.open(file, 'r');
  try {
    const { buffer } = await handle.read(Buffer.alloc(8), 0, 8, 0);
    return buffer.equals(PNG);
  } finally {
    await handle.close();
  }
};

With these settings the 2 MB file was refused with LIMIT_FILE_SIZE, the text declared as PNG was refused with not a PNG file, and a real PNG passed. dest has already written the file to disk before the check, so delete the file you reject. The lab route removed it with fs.promises.rm, and the upload folder held the same number of files before and after one rejection. The 8-byte check looks only at the PNG signature, though. It never checks that anything after the signature is a PNG, so any bytes after those 8 pass: a 26-byte file with a shell script after the signature passed too. Nor does it replace the rest of your security settings, such as authentication, storage location or the Content-Type used when serving the file back. 1 MB is an example value, so choose one for your service. The Security section of the multer README also recommends limits on fields, fieldNestingDepth and fieldArrayIndexLimit, beyond the fileSize and files set here.

When the server gets no file, look first at that request's Content-Type in the developer tools. If it is urlencoded, fix the form's enctype or the URLSearchParams. If it is multipart/form-data with no boundary, fix the header written by hand. If it is application/json, fix the JSON.stringify. If it is multipart/form-data with a boundary but there is no file part, check the file input's name. If there is no Content-Type and file= appears in the URL, check the method. This sorting is the author's summary of the runs above, and setups where a proxy or framework rewrites the request were not run.

DEV NOTES

Same symptom, sorted by Content-Type

What the developer tools show, and what to fix

  • urlencoded

    application/x-www-form-urlencoded

    • Only text went in the file's place (A, H)
    • Add enctype, or drop URLSearchParams
  • multipart, no boundary

    multipart/form-data

    • The parser could not split the parts (F)
    • Remove the Content-Type you set by hand
  • application/json

    application/json

    • The File became {} (G)
    • Send FormData, not JSON.stringify
  • multipart with boundary

    multipart/form-data; boundary=…

    • The type is right (B, E)
    • No file part: check the input's name (D)
    • 0 bytes: check that a file was chosen (I)

The author's sorting of runs A to I above. A GET form (C) sends no Content-Type and no body, and puts file= in the URL. Proxies and frameworks were not run.

Match the Content-Type value from the developer tools to a card title. The ‘multipart with boundary’ card means the type is right; if the file is still missing or empty, check the input's name and whether a file was chosen.
THE END
RSS feedBack to the library
COMMENTS BOX

Add your perspective.

Share a question, another approach, or something you have tried.

Newest first

Checking sign-in…

Loading comments…