用于表单的属性enctype是什么?
<form id='injectFormUpload' enctype='multipart/form-data' action='' method='post'>
答案 0 :(得分:6)
它用于指定用于请求的内容类型。 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--