You are here: Reference > JavaScript > client-side > HTML DOM > properties > encoding (form)

encoding property | enctype property (form)

Browser support:
encoding
enctype
Sets or retrieves how form data should be encoded for submitting to the server.
You need to set this property value to 'multipart/form-data' if the form you want to submit to the server contains an input:file element.
The enctype and the encoding properties are equivalent, older version of browsers only support the encoding property.

Syntax:

object.encoding;
object.enctype;
You can find the related objects in the Supported by objects section below.
encoding: This property is read/write.
enctype: This property is read/write.
HTML page for this property: encType

Possible values:

String that sets or retrieves the type of encoding.
The following values must be supported by browsers:
application/x-www-form-urlencoded
If the form contains text data only, this value can be used.
multipart/form-data
If the form contains files (see input:file) or binary data, this value must be used.
Default: application/x-www-form-urlencoded.

Example HTML code 1:

This example illustrates the use of the encType attribute:
<form method="post" enctype="multipart/form-data" action="#URL#">
    Select a file: <input type="file" name="uploadFile" size="25" />
    <br /><br />
    <input type="submit" value="Upload" />
</form>
Did you find this example helpful? yes no

Example HTML code 2:

This example checks the contents of the file selection element and sets the encoding of the form before submitting:
<head>
    <script type="text/javascript">
        function CheckAndSubmit () {
            var uploadForm = document.getElementById ("uploadForm");
            var uploadFile = document.getElementById ("uploadFile");

            if (uploadFile.value.length == 0) {
                alert ("Please specify the path to the file to upload!");
                return;
            }

            uploadForm.encoding = "multipart/form-data";
            uploadForm.submit ();
        }
    </script>
</head>
<body>
    <form id="uploadForm" method="post" action="#URL#">
        Select a file: <input type="file" name="uploadFile" id="uploadFile" size="25" />
        <br /><br />
        <input type="button" value="Upload" onclick="CheckAndSubmit ();" />
    </form>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content