Multipart field for uploading multiple files in the HTTP protocol

Source: Internet
Author: User

Hello everyone, I am Xu Fei, Microsoft Pinyin Development intern. In network programming, it is often used to "download" Some data from the server, sometimes to "upload" data to the server. I used the "multipart/form-data" format in a prototype to upload files to a Web server and share them with you.
------------------------------------------------------------------
Sometimes, you need to upload files to the server during network programming. Multipart/form-data is a way to upload files.

Multipart/form-data is actually the way that browsers upload files using forms. The most common scenario is when you write a message, add an attachment to the message, and the attachment is usually added using a form that is uploaded to the server in multipart/form-data format.

Upload Attachments in form form

What are the specific steps?

first, the client and the server establish a connection ( TCP protocol).

Second, the client can send data to the server side. Because uploading a file is essentially sending a request to the server side.

third, the client complies with the " Multipart/form-data "format to send data to the server side.

What is the format of the Multipart/form-data?

Since the Multipart/form-data format is the format in which browsers submit data using forms, let's look at what the file looks like after it's been encoded by the browser.

HTML form

Browser Open Form

Click "Browse ..." to select "Unknow.gif" and "unknow1.gif" files respectively, click "Submit" button, the file will be uploaded to the server.

The following is the data received by the server:

Data received by the server

This is a POST request. So the data is placed in the request body, not within the request header.

This line indicates that the request is in the "Multipart/form-data" format, and that "boundary" is the string "---------------------------7db15a14291cce".

It is not hard to imagine that "boundary" is used to separate data from different parts of the form. The form in the example has 2 parts of data, separated by "boundary". "Boundary" is usually randomly generated by the system, but it can be replaced simply with "-------------".

In fact, the beginning of each part of the data begins with "--" + boundary , rather than starting with boundary. Look closely to find out the beginning of this string is actually more than boundary "--"

The boundary is followed by a description of that part of the data.

The next is the data.

"GIF" GIF format picture of the file header, visible, Unknow1.gif is indeed a GIF format picture.

At the end of the request, the "--" + Boundary + "--" indicates the end of the form .

It is important to note that in the HTML protocol, "/r/n" is wrapped instead of "/n".

Multipart field for uploading multiple files in the HTTP protocol

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.