Open In App

What does enctype=’multipart/form-data’ mean in an html form ?

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

When building a web application, it’s common to include forms that allow users to submit information. HTML forms can be used to send data to a server for processing, but it’s important to specify the encoding type of the form. The enctype attribute is used to specify the encoding type of an HTML form.

The enctype attribute specifies how the form data should be encoded before it’s sent to the server. The default value is “application/x-www-form-urlencoded”, which means that the data is encoded in a format that is compatible with URLs. However, when the form includes files, the enctype attribute needs to be set to “multipart/form-data”.

The “multipart/form-data” encoding type is used for forms that include binary data, such as an image or audio files. When a user submits a form with “multipart/form-data” encoding, the data is split into multiple parts and sent to the server in a way that preserves the binary data. The data is then reassembled by the server and processed accordingly.

Here are two examples of HTML forms that use “multipart/form-data” encoding:

Example 1: Simple Form: In this example, we will create a simple HTML form with a file input field. When the form is submitted, the file will be uploaded to the server using the ‘multipart/form-data’ encoding type.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Example 1: Simple Form</title>
</head>
  
<body>
    <h1 style="color:green">
          Geeks for Geeks
      </h1>
    <form action="/upload" method="post" 
          enctype="multipart/form-data">
        <label for="file">Select a file:</label>
        <input type="file" name="file" id="file">
        <input type="submit" value="Upload">
    </form>
</body>
</html>


Output: In this example, we’ve used the enctype attribute with the value ‘multipart/form-data’. The form action attribute is set to “/upload”, which is the URL where the form data will be sent when the form is submitted. The file input field is created using the input element with the type attribute set to ‘file’ and the name attribute set to ‘file’. When the user selects a file using this input field and submits the form, the file will be uploaded to the server.

Simple Form where we can upload only one file

Example 2: Uploading Multiple Files: In this example, we will create an HTML form that allows users to upload multiple files to the server. When the form is submitted, all the files will be uploaded to the server using the ‘multipart/form-data’ encoding type.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Example 2: Uploading Multiple Files</title>
</head>
  
<body>
    <h1 style="color:green">
        Geeks for Geeks
    </h1>
    <form action="/upload" method="post" 
          enctype="multipart/form-data">
        <label for="files">Select files:</label>
        <input type="file" name="files[]" id="files" multiple>
        <input type="submit" value="Upload">
    </form>
</body>
</html>


Output: In this example, we’ve used the ‘multiple’ attribute with the file input field to allow users to select multiple files. The name attribute is set to ‘files[]’, which allows the server-side code to access the uploaded files as an array. When the user selects one or more files using this input field and submits the form, all the files will be uploaded to the server.

Simple Form where we can upload multiple files

These examples demonstrate how to use the enctype attribute with the value ‘multipart/form-data’ in HTML forms to upload files and other types of binary data to the server. By following these examples, you can build forms that allow users to upload files and other types of binary data to your web applications.

In summary, the enctype attribute is used to specify the encoding type of an HTML form. When a form includes binary data, such as image or audio files, the enctype should be set to “multipart/form-data” to ensure that the data is uploaded correctly. By understanding how to set the enctype attribute correctly, web developers can ensure that their forms work correctly and provide a good user experience.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads