2
用於表單的屬性enctype是什麼?爲什麼使用表單enctype?
<form id='injectFormUpload' enctype='multipart/form-data' action='' method='post'>
用於表單的屬性enctype是什麼?爲什麼使用表單enctype?
<form id='injectFormUpload' enctype='multipart/form-data' action='' method='post'>
它用於指定用於請求的內容類型。在html表單中使用了兩種內容類型:默認爲application/x-www-form-urlencoded
,如果表單包含用於上傳文件的文件輸入,則使用multipart/form-data
。它指示瀏覽器如何將請求發送到服務器。例如:
<form id="injectFormUpload" action="" method="post">
<input type="text" name="foo" value="bar" />
<input type="text" name="foo2" value="baz" />
<input type="submit" value="OK" />
</form>
提交將發送以下POST請求到服務器時:
POST/HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 16
Connection: keep-alive
foo=bar&foo2=baz
,而下面的表格包含一個文件上傳字段:
<form id="injectFormUpload" action="" method="post" enctype="multipart/form-data">
<input type="text" name="foo" value="bar" />
<input type="file" name="myfile" />
<input type="submit" value="OK" />
</form>
可能產生的以下要求:
POST/HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Content-Type: multipart/form-data; boundary=---------------------------265001916915724
Content-Length: 326
Connection: keep-alive
-----------------------------265001916915724
Content-Disposition: form-data; name="foo"
bar
-----------------------------265001916915724
Content-Disposition: form-data; name="myfile"; filename="test.txt"
Content-Type: text/plain
contents of the text file
-----------------------------265001916915724--