Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

enctype: the encoding of form submission

Use multipart encoding for files and let the browser create its boundary.

Version: HTML Living Standard

Syntax

<form method="post" enctype="multipart/form-data">...</form>

The form attribute enctype selects submission encoding. Values are application/x-www-form-urlencoded, multipart/form-data and text/plain; missing or invalid values use the first. A submitting button can override the form setting. The encoding is relevant to the submission algorithm, especially the body of an HTTP POST, not a promise that GET sends a multipart body.

Use multipart encoding when sending file contents. The browser supplies the boundary separating parts; do not invent a mismatched Content-Type header when sending FormData with JavaScript. Plain-text encoding is ambiguous and unsuitable for robust structured processing. Validate names, sizes and uploaded contents on the server.

The fixture generates a small test file and submits a real multipart POST. The local endpoint checks the content type, field, filename and file bytes. This verifies the wire representation, not merely the form’s attribute value.

Examples

<!doctype html><meta charset="utf-8">
<form id="form" action="/upload" method="post" enctype="multipart/form-data"><label>Title <input name="title" value="Example"></label><label>File <input id="file" name="document" type="file"></label><button>Send local fixture</button></form>
<script>
// A generated test file, never a file read from the user's device.
const files = new DataTransfer(); files.items.add(new File(['Local file.'], 'note.txt', { type: 'text/plain' }));
document.querySelector('#file').files = files.files;
// The isolated local endpoint validates the actual multipart request and returns its result.
</script>

Result

multipart=true; filename=note.txt; text=Local file.

The upload is generated in memory and sent only to the isolated intercepted endpoint.

Sources