What is Enctype Multipart Form Data in HTML

In HTML Forms, the enctype attribute used to handle encoding of form submitted data. When we use form GET or POST action, we have to encode the form data to get form submitted data. The HTML forms provide three methods of encoding. The default is application/x-www-form-urlencoded, which is more or less the same as a query string on the end of the URL. The multipart/form-data is a more complicated encoding but one which allows entire files to be included in the form data.

So when we have a HTML form with input type file to upload files, then we need to pass multipart/form-data with form. In short, we can use it when we have an <input type="file" />.

There are three form enctypeAttribute values to use with HTML form. We will explain each in details.

1. application/x-www-form-urlencoded

This is default enctype attribute value and there no need to pass attribute enctype with this value if needed default form data encoding. With this attribute value, all characters are encoded before sending. The spaces are encoded with + symbols and special characters are encoded to ASCII HEX values.

2. multipart/form-data

When we use enctype attribute value multipart/form-data, the characters are not encoded. The form data transmitted as a MIME stream. This enctype attribute value is used when we need to use form with input type file to upload files.

<form action=“action.php” method=“get” enctype=“multipart/form-data”>
Name: <input type=“text” name=“u_name” />
Profile photo: <input type=“file” name=“profile_image” />
<input type=“submit” value=“Submit” />

3. text/plain

The enctype attribute value text/plain is used transmit form data with encoding. When we use this attribute value, only spaces are converted to + symbols and there no special characters encoded.

<form action=“action.php” method=“get” enctype=“text/plain”>
Name: <input type=“text” name=“name” />
Phone: <input type=“number” name=“phone” />
<input type=“submit” value=“Submit” />

You may like these posts