HTML form enctype 属性试验

本文详细介绍了HTML表单中的enctype属性的作用及其不同值的应用场景,特别是当表单包含文件上传时如何正确设置enctype。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTML form enctype

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1%E2%80%8D

 

enctype= content-type [CI]
This attribute specifies the content type used to submit the form to the server (when the value of method is "post"). The default value for this attribute is "application/x-www-form-urlencoded". The value "multipart/form-data" should be used in combination with the INPUT element,type="file".

 

enctype 如果不写, 默认为 application/x-www-form-urlencoded, 如果提交内容包括 file input元素, 则类型应该是 multipart/form-data

 

enctype含义?

且看下面描述, 浏览器处理表单过程的第三步,将表单数据集合编码, 编码是根据enctype指定的content-type值。 编码格式是HTTP协议的一部分, 客户端和服务器都遵守。

Step three: Encode the form data set 

The form data set is then encoded according to the content type specified by the enctype attribute of the FORM element.

 

但是 enctype是否就这两种类型?

看规范描述, 只要求UA支持这两种类型, 其他的content-type类型没有定义。

17.13.4 Form content types

The enctype attribute of the FORM element specifies the content type used to encode the form data set for submission to the server. User agents must support the content types listed below. Behavior for other content types is unspecified.

。。。

application/x-www-form-urlencoded  

This is the default content type. Forms submitted with this content type must be encoded as follows:

。。。。

multipart/form-data 

。。。

 

enctype用法试验

首先,只有使用POST方法的时候enctype才生效,GET方法默认使用 application/x-www-form-urlencoded 编码方法。

  • If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a`?'to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type. The user agent then traverses the link to this URI. In this scenario, form data are restricted to ASCII codes.
  • If the method is "post" and the action is an HTTP URI, the user agent conducts an HTTP "post" transaction using the value of the action attribute and a message created according to the content type specified by the enctype attribute.

 

构造一个含有file控件的form, 还包括一个input框, 变化enctype为以下值, 查看请求报文都是按照 application/x-www-form-urlencoded 编码的:

1、 不书写 enctype 属性

<form id="fileupload" name="fileupload" method="post" action="/index.php">

2、 书写enctype但是值为空:

<form id="fileupload" name="fileupload" method="post" action="/index.php" enctype="">

3、 书写enctype值为未定义值:

<form id="fileupload" name="fileupload" method="post" action="/index.php" enctype="sss">

4、 书写enctype为

<form id="fileupload" name="fileupload" method="post" action="/index.php" enctype="application/x-www-form-urlencoded">

 

代码:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./jquery.js"></script>
    <link rel="stylesheet" href="./test.css" />
</head>
<body>
    <style>
    </style>
    <form id="fileupload" name="fileupload" method="post" action="/index.php" enctype="sss">
        <input type="file" name="testfile"></br>
        <input type="text" name="textinput" value="textinputvalue"></br>
        <input type="submit" value="upload"></br>
    </form>
    <script>

    </script>
</body>
</html>

 

抓包:

POST http://wwwtestcom/index.php HTTP/1.1
Host: wwwtestcom
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 0
Referer: http://wwwtestcom/fileupload.php
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 56

testfile=%E5%91%A8%E6%8A%A5.txt&textinput=textinputvalue

 

修改enctype为 multipart/form-data

<form id="fileupload" name="fileupload" method="post" action="/index.php" enctype="multipart/form-data">

抓包:

POST http://wwwtestcom/index.php HTTP/1.1
Host: wwwtestcom
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 0
Referer: http://wwwtestcom/fileupload.php
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------734115918637
Content-Length: 299

-----------------------------734115918637
Content-Disposition: form-data; name="testfile"; filename="test.txt"
Content-Type: text/plain


-----------------------------734115918637
Content-Disposition: form-data; name="textinput"

textinputvalue
-----------------------------734115918637--

 

另外, 演示下file控件可以选择多个文件的情况:

代码:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./jquery.js"></script>
    <link rel="stylesheet" href="./test.css" />
</head>
<body>
    <style>
    </style>
    <form id="fileupload" name="fileupload" method="post" action="/index.php" enctype="multipart/form-data">
        <input type="file" name="testfile" multiple="multiple"></br>
        <input type="text" name="textinput" value="textinputvalue"></br>
        <input type="submit" value="upload"></br>
    </form>
    <script>

    </script>
</body>
</html>

选择两个文件, 抓包 -- 可以看到报文体中, 有两个 testfile 的PART, 每个PART对应一个文件:

POST http://wwwtestcom/index.php HTTP/1.1
Host: wwwtestcom
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 0
Referer: http://wwwtestcom/fileupload.php
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------25276621921226
Content-Length: 451

-----------------------------25276621921226
Content-Disposition: form-data; name="testfile"; filename="test.txt"
Content-Type: text/plain


-----------------------------25276621921226
Content-Disposition: form-data; name="testfile"; filename="test1.txt"
Content-Type: text/plain


-----------------------------25276621921226
Content-Disposition: form-data; name="textinput"

textinputvalue
-----------------------------25276621921226--

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值