表单的enctype属性是用来做什么的?
<form id='injectFormUpload' enctype='multipart/form-data' action='' method='post'>发布于 2011-10-25 02:51:02
它用于指定请求所使用的内容类型。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--https://stackoverflow.com/questions/7880341
复制相似问题