Brother, this is called multipart !, Brother multipart

Source: Internet
Author: User
Tags alphanumeric characters

Brother, this is called multipart !, Brother multipart

Brother, this is called multipart!

Beautiful Life of the sun and fire god (http://blog.csdn.net/opengl_es)

This article follows the "signature-non-commercial use-consistency" creation public agreement

Reprinted please keep this sentence: Sun huoshen's beautiful life-this blog focuses on Agile development and mobile and IOT device research: iOS, Android, Html5, Arduino, pcDuino, otherwise, this blog post is rejected or reprinted. Thank you for your cooperation.



What will you do if I only tell you how to upload files using the http protocol?

There may be multiple methods, but at least we need to know that the http GET method is not reliable, too short;

Therefore, POST is undoubtedly a perfect choice for Http methods, because its package body can be infinitely long content.

To put the file content in the package body after BASE64 encoding, or upload the file using multipart/form-data.


The following is an excerpt from the html401 protocol. For more details, refer to rfc1341.


17.13.4 Form content types

TheEnctypeAttribute ofFORMElement 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'+', And then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced'% 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'='And name/value pairs are separated from each other'&'.
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" shocould 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 shocould 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 shoshould 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 wocould 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 shoshould 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 shoshould be returned as "multipart/mixed" embedded within the "multipart/form-data ".

The user agent shocould 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 the 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 extends strates "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--







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.