The HTML formenctype attribute
The HTML formenctype attribute sets the encoding used when one specific button submits a form with POST. Use formenctype="multipart/form-data" on an upload button so file contents are sent. It overrides the form's enctype and has no effect on GET submissions.
Overview
The HTML formenctype attribute sets how form data is encoded when one particular submit button sends the form. It overrides the form's enctype for that submission and accepts the same three values, application/x-www-form-urlencoded, multipart/form-data and text/plain.
The encoding only matters for POST. A GET submission always puts the fields in the URL's query string, so a formenctype on a GET button is ignored. Pair it with formmethod="post" unless the form already posts. An unknown value, such as application/json, falls back to URL encoding.
The usual reason to reach for it is a file. With the default URL-encoded format, a file input sends nothing but the file's name. Setting formenctype="multipart/form-data" on an upload button sends the file's contents, while the other buttons in the same form keep the form's encoding.
Avoid text/plain outside debugging. The HTML standard describes it as a format meant for humans that a program can't reliably parse, because nothing separates a line break inside a value from the one that ends it.
Syntax
<form action="/profile" method="post">
<label>Name <input type="text" name="display_name"></label>
<label>Photo <input type="file" name="avatar"></label>
<button>Save name</button>
<button formenctype="multipart/form-data" formaction="/profile/avatar">Upload photo</button>
</form>
Values
| Value |
|---|
| application/x-www-form-urlencoded | multipart/form-data | text/plain |
Example
<form id="up" action="/upload" method="post">
<input type="file" name="photo" aria-label="Photo">
<button>Default</button>
<button formenctype="multipart/form-data">Multipart</button>
</form>
<p id="out">Pick a file, then click a button.</p>
<script>
document.getElementById('up').addEventListener('submit', function (e) {
e.preventDefault();
var type = e.submitter.getAttribute('formenctype') || this.enctype;
var note = type === 'multipart/form-data' ? 'so the file itself is sent.' : 'so only the file name is sent.';
document.getElementById('out').textContent = 'This button encodes the form as ' + type + ', ' + note;
});
</script>
Best practices
- Use
formenctypeonly when one button needs a different encoding. If every submission uploads files, setenctypeon the form instead. - Always pair
formenctypewith a POST method, either on the form or through formmethod. - Check what the server expects. PHP, for example, only fills
$_FILESfor uploads sent asmultipart/form-data. - Keep
text/plainout of production forms.
Frequently asked questions
What does formenctype change when a button submits a form?
Why does my form upload send only the file name?
formmethod=post and formenctype=multipart/form-data on the upload button, or the matching attributes on the form.Does formenctype work with GET?
formmethod=post.When is formenctype useful?
Can formenctype send JSON?
Should I use text/plain with formenctype?
formenctype=text/plain for debugging. The HTML standard warns that text/plain payloads can't be reliably read by a program.