multi-part/form-data

web 2007. 8. 23. 03:34

제 자신을 위해서 좀 상세하게 씁니다.

 

<html>
<head>
<title>File manipulation test</title>
</head>

<body>
<form name="uploadForm" method="post" action="upload.jsp">
이름<input type=text name="name"><br>
전화번호
<input type=text name="phone"><br>
사진
<input type=file name="att"><br>
<input type=submit>
</form>
</body>
</html>

 

이것을 가능하게 만들기 위한 방법이 multipart/form-data 이며, 클라이언트(HTML) 서버모듈을 모두 고쳐주어야 하지요.

<html>
<head>
<title>File manipulation test</title>
</head>

<body>
<form name="uploadForm" method="post"
enctype="multipart/form-data"
action="upload.jsp">
이름<input type=text name="name"><br>
전화번호
<input type=text name="phone"><br>
사진
<input type=file name="att"><br>
<input type=submit>
</form>
</body>
</html>

 

서버에 다음의 형식으로 전송됩니다.

-----------------------------7d13603712507ca
Content-Disposition: form-data; name="name"
김용환
-----------------------------7d13603712507ca

Content-Disposition: form-data; name="phone"111-1111

-----------------------------7d13603712507ca

Content-Disposition: form-data; name="att"; filename="a.txt"

Content-Type: text/plain
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

 

스펙(http://www.w3.org/TR/html401/interact/forms.html#adef-enctype)을 보니까 enctype은 다음과 같이 나와있군요.

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".

 

17.13.4에 잘 정리되어 있습니다.. 참고하세요

17.13.4 Form content types

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.

Please also consult the section on escaping ampersands in URI attribute values.

application/x-www-form-urlencoded  

This is the default content type. Forms submitted with this content type must be encoded as follows:

  1. Control names and values are escaped. Space characters are replaced by `+', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by `%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A').
  2. The control names/values are listed in the order they appear in the document. The name is separated from the value by `=' and name/value pairs are separated from each other by `&'.

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.

Please consult the appendix for information about security issues for forms.

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

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.

A "multipart/form-data" message contains a series of parts, each representing a successful control. The parts are sent to the processing agent in the same order the corresponding controls appear in the document stream. Part boundaries should not occur in any of the data; how this is done lies outside the scope of this specification.

As with all multipart MIME types, each part has an optional "Content-Type" header that defaults to "text/plain". User agents should supply the "Content-Type" header, accompanied by a "charset" parameter.

Each part is expected to contain:

  1. a "Content-Disposition" header whose value is "form-data".
  2. a name attribute specifying the control name of the corresponding control. Control names originally encoded in non-ASCII character sets may be encoded using the method outlined in [RFC2045].

Thus, for example, for a control named "mycontrol", the corresponding part would be specified:

Content-Disposition: form-data; name="mycontrol"

As with all MIME transmissions, "CR LF" (i.e., `%0D%0A') is used to separate lines of data.

Each part may be encoded and the "Content-Transfer-Encoding" header supplied if the value of that part does not conform to the default (7BIT) encoding (see [RFC2045], section 6)

If the contents of a file are submitted with a form, the file input should be identified by the appropriate content type (e.g., "application/octet-stream"). If multiple files are to be returned as the result of a single form entry, they should be returned as "multipart/mixed" embedded within the "multipart/form-data".

The user agent should attempt to supply a file name for each submitted file. The file name may be specified with the "filename" parameter of the 'Content-Disposition: form-data' header, or, in the case of multiple files, in a 'Content-Disposition: file' header of the subpart. If the file name of the client's operating system is not in US-ASCII, the file name might be approximated or encoded using the method of [RFC2045]. This is convenient for those cases where, for example, the uploaded files might contain references to each other (e.g., a TeX file and its ".sty" auxiliary style description).

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:

   Content-Type: multipart/form-data; boundary=AaB03x   --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:

   Content-Type: multipart/form-data; boundary=AaB03x   --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--

 

Posted by '김용환'

댓글을 달아 주세요