带有/文件输入字段 HTTP 请求的表单是什么样的?
What does a form w/ file input field HTTP's request look like?
带有附加文件的 HTTP header 是什么样的(客户端到服务器文件)以及我如何解析它,例如在 .NET 中(小问题,对 RegEx 和 Streams 等有经验)
从 HTML 网络表单上传文件使用 HTTP POST
请求和 MIME multipart/form-data
内容类型,这在 HTML 规范中定义(我已经在规范中显示的示例中添加了 HTTP headers):
HTML4: Section 17 Forms - 17.13.4 Form content types - multipart/form-data
Note. Please consult [RFC2388] for additional information about file uploads, including backwards compatibility issues, the relationship between "multipart/form-data" and other content types, performance issues, etc.
...
The content "multipart/form-data" follows the rules of all multipart MIME data streams as outlined in [RFC2045]. The definition of "multipart/form-data" is available at the [IANA] registry.
...
The following example illustrates "multipart/form-data" encoding. Suppose we have the following form:
<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>
If the user enters "Larry" in the text input, and selects the text file "file1.txt", the user agent might send back the following data:
POST /cgi/handle HTTP/1.1
Host: server.com
Content-Type: multipart/form-data; boundary=AaB03x
Content-Length: ...
--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--
If the user selected a second (image) file "file2.gif", the user agent might construct the parts as follows:
POST /cgi/handle HTTP/1.1
Host: server.com
Content-Type: multipart/form-data; boundary=AaB03x
Content-Length: ...
--AaB03x
Content-Disposition: form-data; name="submit-name"
Larry
--AaB03x
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y
--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary
...contents of file2.gif...
--BbC04y--
--AaB03x--
HTML5: Section 4.10 Forms - 4.10.22 Form submission - 4.10.22.7 Multipart form data
On the other hand, consider this form:
<form action="/find.cgi" method=post enctype="multipart/form-data">
<input type=text name=t>
<input type=search name=q>
<input type=submit>
</form>
Given the same user input, the result on submission is quite different: the user agent instead does an HTTP POST to the given URL, with as the entity body something like the following text:
POST /find.cgi HTTP/1.1
Host: server.com
Content-Type: multipart/form-data; boundary=----kYFrd4jNJEgCervE
Content-Length: ...
------kYFrd4jNJEgCervE
Content-Disposition: form-data; name="t"
cats
------kYFrd4jNJEgCervE
Content-Disposition: form-data; name="q"
fur
------kYFrd4jNJEgCervE--
...
The order of parts must be the same as the order of fields in the form data set. Multiple entries with the same name must be treated as distinct fields.
Note: In particular, this means that multiple files submitted as part of a single <input type=file multiple>
element will result in each file having its own field; the "sets of files" feature ("multipart/mixed") of RFC 2388 is not used.
阅读以下 RFC,了解有关一般如何处理 HTTP 和 MIME 的更多详细信息:
RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies
RFC 2046 Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types
(特别是 Section 5.1 Multipart Media Type)
带有附加文件的 HTTP header 是什么样的(客户端到服务器文件)以及我如何解析它,例如在 .NET 中(小问题,对 RegEx 和 Streams 等有经验)
从 HTML 网络表单上传文件使用 HTTP POST
请求和 MIME multipart/form-data
内容类型,这在 HTML 规范中定义(我已经在规范中显示的示例中添加了 HTTP headers):
HTML4: Section 17 Forms - 17.13.4 Form content types - multipart/form-data
Note. Please consult [RFC2388] for additional information about file uploads, including backwards compatibility issues, the relationship between "multipart/form-data" and other content types, performance issues, etc.
...
The content "multipart/form-data" follows the rules of all multipart MIME data streams as outlined in [RFC2045]. The definition of "multipart/form-data" is available at the [IANA] registry.
...
The following example illustrates "multipart/form-data" encoding. Suppose we have the following form:
<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>
If the user enters "Larry" in the text input, and selects the text file "file1.txt", the user agent might send back the following data:
POST /cgi/handle HTTP/1.1 Host: server.com Content-Type: multipart/form-data; boundary=AaB03x Content-Length: ... --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--
If the user selected a second (image) file "file2.gif", the user agent might construct the parts as follows:
POST /cgi/handle HTTP/1.1 Host: server.com Content-Type: multipart/form-data; boundary=AaB03x Content-Length: ... --AaB03x Content-Disposition: form-data; name="submit-name" Larry --AaB03x Content-Disposition: form-data; name="files" Content-Type: multipart/mixed; boundary=BbC04y --BbC04y Content-Disposition: file; filename="file1.txt" Content-Type: text/plain ... contents of file1.txt ... --BbC04y Content-Disposition: file; filename="file2.gif" Content-Type: image/gif Content-Transfer-Encoding: binary ...contents of file2.gif... --BbC04y-- --AaB03x--
HTML5: Section 4.10 Forms - 4.10.22 Form submission - 4.10.22.7 Multipart form data
On the other hand, consider this form:
<form action="/find.cgi" method=post enctype="multipart/form-data"> <input type=text name=t> <input type=search name=q> <input type=submit> </form>
Given the same user input, the result on submission is quite different: the user agent instead does an HTTP POST to the given URL, with as the entity body something like the following text:
POST /find.cgi HTTP/1.1 Host: server.com Content-Type: multipart/form-data; boundary=----kYFrd4jNJEgCervE Content-Length: ... ------kYFrd4jNJEgCervE Content-Disposition: form-data; name="t" cats ------kYFrd4jNJEgCervE Content-Disposition: form-data; name="q" fur ------kYFrd4jNJEgCervE--
...
The order of parts must be the same as the order of fields in the form data set. Multiple entries with the same name must be treated as distinct fields.
Note: In particular, this means that multiple files submitted as part of a single
<input type=file multiple>
element will result in each file having its own field; the "sets of files" feature ("multipart/mixed") of RFC 2388 is not used.
阅读以下 RFC,了解有关一般如何处理 HTTP 和 MIME 的更多详细信息:
RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies
RFC 2046 Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types
(特别是 Section 5.1 Multipart Media Type)