表单 x-www-form-urlencoded 与 multipart/form-data 区别
如何正确选择 Form
的 Content-type
类型?
x-www-form-urlencoded
,表单默认的Content-type
类型,支持ASCII-text
文本内容multipart/form-data
,允许提交表单包含:files
,non-ASCII-text
,Binary
类型数据
application/x-www-form-urlencoded
当表单使用 application/x-www-form-urlencoded
时,需要对参数进行urlencode 编码
和序列化
如,表单提交参数(key-value)为:
param1:website
param2:https://www.google.com
经过 urlencode
编码后:
param1:website
param2:https%3A%2F%2Fwww.google.com
再经过序列化,得到结果
param1=website¶m2=https%3A%2F%2Fwww.google.com
multipart/form-data
一个 multipart/form-data
消息体,包含多个块组成,每个块代表一个有效的表单控件,并使用 boundary
的字符串分割:
- 第一部分,
Content-Disposition: form-data
参数名称,如,name="my_control
- 第二部分,
Content-Type: text/plain
,Content-Type: image/png
- 第三部分,消息内容
例如表单:
<FORM action="http://server.com/cgi/handle"
enctype="multipart/form-data"
method="post">
<P>
What is your name? <INPUT type="text" name="submit-name"><BR>
What files are you sending? <INPUT type="file" name="files"><BR>
<INPUT type="submit" value="Send">
<INPUT type="reset">
</FORM>
假设,submit-name
输入 Larry
文本,files
选择文件 file1.txt
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="submit-name"
Larry
--AaB03x
Content-Disposition: form-data; name="files"; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
参考文献:https://www.w3.org/TR/html401/interact/for...
本作品采用《CC 协议》,转载必须注明作者和本文链接