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--

HTML form enctype 属性试验的更多相关文章

  1. HTML 5 <form> enctype 属性

    值 描述 application/x-www-form-urlencoded 在发送前对所有字符进行编码(默认). multipart/form-data 不对字符编码.当使用有文件上传控件的表单时, ...

  2. C# HTTP系列10 form表单的enctype属性

    系列目录     [已更新最新开发文章,点击查看详细] 在ASP.NET编程中经常遇到下面的代码片段,将人员信息以表单方式提交到后台程序并保存到服务器与数据库中. <form action=&q ...

  3. 表单中<form>的enctype属性

    application/x-www-form-urlencoded.multipart/form-data.text/plain 上传文件的表单中<form>要加属性enctype=&qu ...

  4. form表单标签的enctype属性的作用

    Enctype是指定将数据回发到server时浏览器使用的编码类型.其编码类型有下面三种 一. application/x-www-form-urlencoded         这是通过表单发送数据 ...

  5. HTML <form> 标签的 enctype 属性

    HTML <form> 标签 定义和用法 enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www- ...

  6. form标签的 enctype属性

    1.enctype的定义: enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www-form-urlencod ...

  7. Form的enctype属性

    Form的enctype属性 一般都使用html的Form表单通过HTTP POST方法发送Request body.下面是一个form: <form action="/process ...

  8. Form表单标签的Enctype属性的作用及应用示例介绍

    Enctype :指定将数据回发到服务器时浏览器使用的编码类型.用于表单里有图片上传. 编码类型有以下三种: application/x-www-form-urlencoded: 在发送前编码所有字符 ...

  9. HTML表单(form)的“enctype”属性

    Form元素的语法中,EncType表明提交数据的格式 属性值: application/x-www-form-urlencoded:在发送前编码所有字符(默认) multipart/form-dat ...

随机推荐

  1. Android 摇一摇 之 传感器片

    要监视原始的传感器数据,你需要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged(). 传感器数据的速度值,这 ...

  2. 使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法

    使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法 添加一行 manager. ...

  3. HDU-I Hate It

    Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写 ...

  4. iframe高度自适应内容

    JS自适应高度,其实就是设置iframe的高度,使其等于内嵌网页的高度,从而看不出来滚动条和嵌套痕迹.对于用户体验和网站美观起着重要作用. 如果内容是固定的,那么我们可以通过CSS来给它直接定义一个高 ...

  5. js+css立体旋转

    纯 CSS3 制作可口可乐罐  这个效果相信大家很多人看过了,纯css实现的立体可口可乐罐,看起来相当高大上~ 于是今天我这小菜鸟试着研究下,稍微遗憾的是,没有看到源码,还是直接F12吧,貌似实现也不 ...

  6. 两种不同png图片的在项目中的运用

    png图片主要分为两种 png-8和png-24. PNG8和PNG24后面的数字则是代表这种PNG格式最多可以索引和存储的颜色值.”8″代表2的8次方也就是256色,而24则代表2的24次方大概有1 ...

  7. 利用jdk自带的运行监控工具JConsole观察分析Java程序的运行

    利用jdk自带的运行监控工具JConsole观察分析Java程序的运行 原文链接 一.JConsole是什么 从Java 5开始 引入了 JConsole.JConsole 是一个内置 Java 性能 ...

  8. 用clip剪裁实现半圆

    定义和用法 clip 属性剪裁绝对定位元素. 说明 这个属性用于定义一个剪裁矩形.对于一个绝对定义元素,在这个矩形内的内容才可见.出了这个剪裁区域的内容会根据 overflow 的值来处理.剪裁区域可 ...

  9. passing ‘const ’ as ‘this’ argument of ‘’ discards qualifiers 错误处理

    示例程序: #include <iostream> #include <set> using   namespace std ; class   StudentT { publ ...

  10. [LintCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors.You have to paint a ...