What Does Enctype='Multipart/Form-Data' Mean?
What Does Enctype='Multipart/Form-Data' Mean in an Html Form and When Should We Use It? 3 10 Answers When You Make a Post Request, You Have to Encode the Data...
What does enctype='multipart/form-data' mean in an HTML form and when should we use it?
10 Answers
When you make a POST request, you have to encode the data that forms the body of the request in some way.
HTML forms provide three methods of encoding.
application/x-www-form-urlencoded(the default)multipart/form-datatext/plain
Work was being done on adding application/json, but that has been abandoned.
(Other encodings are possible with HTTP requests generated using other means than an HTML form submission. JSON is a common format for use with web services and some still use SOAP.)
The specifics of the formats don't matter to most developers. The important points are:
- Never use
text/plain.
When you are writing client-side code:
- use
multipart/form-datawhen your form includes any<input type="file">elements - otherwise you can use
multipart/form-dataorapplication/x-www-form-urlencodedbutapplication/x-www-form-urlencodedwill be more efficient
When you are writing server-side code:
- Use a prewritten form handling library
Most (such as Perl's CGI->param or the one exposed by PHP's $_POST superglobal) will take care of the differences for you. Don't bother trying to parse the raw input received by the server.
Sometimes you will find a library that can't handle both formats. Node.js's most popular library for handling form data is body-parser which cannot handle multipart requests (but has documentation that recommends some alternatives which can).
If you are writing (or debugging) a library for parsing or generating the raw data, then you need to start worrying about the format. You might also want to know about it for interest's sake.
application/x-www-form-urlencoded is more or less the same as a query string on the end of the URL.
multipart/form-data is significantly more complicated but it allows entire files to be included in the data. An example of the result can be found in the HTML 4 specification.
text/plain is introduced by HTML 5 and is useful only for debugging — from the spec: They are not reliably interpretable by computer — and I'd argue that the others combined with tools (like the Network Panel in the developer tools of most browsers) are better for that).
when should we use it?
Quentin's answer is right: use multipart/form-data if the form contains a file upload, and application/x-www-form-urlencoded otherwise, which is the default if you omit enctype.
I'm going to:
- add some more HTML5 references
- explain why he is right with a form submit example
Must Read
HTML5 references
There are three possibilities for enctype:
application/x-www-form-urlencodedmultipart/form-data(spec points to RFC7578)text/plain. This is "not reliably interpretable by computer", so it should never be used in production, and we will not look further into it.