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

Detailed process of changing apt source to Alibaba Cloud source in Ubuntu 18.04

Table of contents Preface: Ubuntu 18.04 changes a...

MySQL column to row conversion tips (share)

Preface: Because many business tables use design ...

jQuery implements simple button color change

In HTML and CSS, we want to set the color of a bu...

HTML+CSS to achieve simple navigation bar function

Without further ado, I'll go straight to the ...

Install docker offline by downloading rpm and related dependencies using yum

You can use yum to install all dependencies toget...

Understanding innerHTML

<br />Related articles: innerHTML HTML DOM i...

CSS pixels and solutions to different mobile screen adaptation issues

Pixel Resolution What we usually call monitor res...

How to print highlighted code in nodejs console

Preface When the code runs and an error occurs, w...

Distributed monitoring system Zabbix uses SNMP and JMX channels to collect data

In the previous article, we learned about the pas...

vsCode generates vue templates with one click

1. Use the shortcut Ctrl + Shift + P to call out ...

Design theory: On the issues of scheme, resources and communication

<br />This problem does not exist in many sm...

Public free STUN servers

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

How to solve the margin collapse problem in CSS

First, let's look at three situations where m...