HTML form enctype 屬性實驗

來源:互聯網
上載者:User

標籤:

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 屬性實驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.