Brother, it's called multipart!

Source: Internet
Author: User
Tags alphanumeric characters

Brother, it's called multipart!

The beautiful Life of the Sun Vulcan (

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or re-reproduced, thank you for your cooperation.

What would you do if I only told you that I used the HTTP protocol to upload files?

There may be many ways, but at least we need to know that the GET method of the HTTP protocol is not reliable, too short;

Then POST is undoubtedly a good choice for Http methods, because it can be an infinitely long body of content.

As for the content of the file is BASE64 encoded into the package body, or use multipart/form-data upload, this look you like.

The following is an excerpt from the HTML401 protocol, which you can refer to rfc1341 for a more detailed description.

17.13.4Form 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 sections on escaping ampersands in URI attribute values.


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

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

Note. Consult [RFC2388] For additional information about file uploads, including backwards compatibility issues, the RelA Tionship between "Multipart/form-data" and other content types, performance issues, etc.

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

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text co Ntaining non-ascii characters. The content type "Multipart/form-data" should is 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 is 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 any multipart MIME types, each of the have an optional "Content-type" header, the defaults to "Text/plain". User agents should supply the "Content-type" header, accompanied by a "charset" parameter.

Each of the 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 could 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‘ ) are used to separate lines of data.

Each of the encoded and the "content-transfer-encoding" header supplied if the value of the, the does not conform to The default (7BIT) encoding (see [RFC2045], section 6)

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

The user agent should attempt to supply a, file name for each submitted file. The file name is specified with the "filename" parameter of the ' Content-disposition:form-data ' header, or, the CA SE 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 all 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://"       enctype= "Multipart/form-data"       method= "POST" >   < p> What's   your name? <input type= "text" name= "Submit-name" ><BR> What's   files is sending? <i Nput 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 FO Llowing 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--

Brother, it's called multipart!

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