Introduction to the Enctype attribute of the Form tag and its application examples

Introduction to the Enctype attribute of the Form tag and its application examples
Enctype : Specifies the type of encoding the browser uses when posting data back to the server. Used for uploading pictures in forms.

There are three encoding types:

application/x-www-form-urlencoded: Encode all characters before sending (default). This is the standard encoding format.
multipart/form-data: Do not encode characters. This value must be used when using a form that contains a file upload control.
text/plain: The form data is encoded as plain text, without any controls or formatting characters.

example:

Copy code
The code is as follows:

<form action="${pageContext.request.contextPath}/imageUpload_saveOrUpdate.action" method="post" enctype="multipart/form-data">
<div>
<label>Please select the upload image address:</label>
<input type="file" name="image"/>
</div>
</div>
<div>
<input type="submit" value="Upload"/>
</div>
</form>

The meaning of enctype="multipart/form-data" in the form is to set the MIME encoding of the form. By default, the encoding format is application/x-www-form-urlencoded, which cannot be used for file uploads; only when multipart/form-data is used can the file data be fully transmitted.

enctype="multipart/form-data" is used to upload binary data.

If you want to obtain the value of the corresponding form field through the Request object on the server side, you should set the enctype attribute to application/x-www-form-urlencoded (that is, the default value, which can be omitted).

Why do you need to set enctype="multipart/form-data" when uploading files?

Because: after setting enctype to multipart/form-data, if the characters are not encoded, the data is transmitted to the server in binary form. At this time, if you use request, you cannot directly get the value of the corresponding form. Instead, you should use the stream object to decode the binary data transmitted to the server and read the data.

If you want to upload a file, you must set the encotype to multipart/form-data.

<<:  Detailed Example of CSS3 box-shadow Property

>>:  Vue implements internationalization of web page language switching

Recommend

Public free STUN servers

Public free STUN servers When the SIP terminal us...

js and jquery to achieve tab status bar switching effect

Today we will make a simple case, using js and jq...

MySQL5.7 single instance self-starting service configuration process

1.MySQL version [root@clq system]# mysql -v Welco...

HTML table tag tutorial (23): row border color attribute BORDERCOLORDARK

In rows, dark border colors can be defined indivi...

How to install vim editor in Linux (Ubuntu 18.04)

You can go to the Ubuntu official website to down...

Docker+gitlab+jenkins builds automated deployment from scratch

Table of contents Preface: 1. Install Docker 2. I...

CentOS server security configuration strategy

Recently, the server has been frequently cracked ...

About the problems of congruence and inequality, equality and inequality in JS

Table of contents Congruent and Incongruent congr...

Web design reference firefox default style

Although W3C has established some standards for HT...

A brief discussion on Yahoo's 35 rules for front-end optimization

Abstract: Whether at work or in an interview, opt...

How to use the Linux basename command

01. Command Overview basename - strip directories...