How to upload a file using the Multipart/form-data format (POST request, the data is placed in the request body, not in the request header, in the HTML protocol, with "\ r \ n" line, instead of "\ n")

Source: Internet
Author: User

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.

Thirdly, the client sends data to the server side in a format that conforms to "Multipart/form-data".

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

The following code snippet shows how to construct the Multipart/form-data format data and upload the image to the server.

//---------------------------------------

The the demo code of using Multipart/form-data to upload text and photos.

-use WinInet APIs.

//

//

Connection handlers.

//

HRESULT hr;

Hinternet M_hopen;

Hinternet M_hconnect;

Hinternet m_hrequest;

//

Make connection.

//

...

//

form the content.

//

Std::wstring strboundary = std::wstring (L "------------------");

Std::wstring Wstrheader (L "Content-type:multipart/form-data, boundary=");

Wstrheader + = strboundary;

Httpaddrequestheaders (M_hrequest, Wstrheader.c_str (), DWORD (Wstrheader.size ()), http_addreq_flag_add);

//

"Std::wstring Strphotopath" is the name of the photo to upload.

//

//

Uploaded photo Form-part begin.

//

Std::wstring Strmultipartfirst (L "--");

Strmultipartfirst + = strboundary;

Strmultipartfirst + = L "\r\ncontent-disposition:form-data; Name=\ "Pic\"; Filename= ";

Strmultipartfirst + = l "\" "+ Strphotopath + L" \ "";

Strmultipartfirst + = L "\r\ncontent-type:image/jpeg\r\n\r\n";

//

"Std::wstring strtextcontent" is the text to uploaded.

//

//

Uploaded text Form-part begin.

//

Std::wstring Strmultipartinter (L "\r\n--");

Strmultipartinter + = strboundary;

Strmultipartinter + = L "\r\ncontent-disposition:form-data; Name=\ "status\" \r\n\r\n ";

Std::wstring Wstrpostdataurlencode (Cencodetool::encode_url (strtextcontent));

Add text content to send.

Strmultipartinter + = Wstrpostdataurlencode;

Std::wstring strmultipartend (L "\r\n--");

Strmultipartend + = strboundary;

Strmultipartend + = L "--\r\n";

//

Open photo file.

//

Ws2s (std::wstring)

-transform "Strphotopath" from Unicode to ANSI.

Std::ifstream *pstdofspicinput = new Std::ifstream;

Pstdofspicinput->open ((Ws2s (Strphotopath)). C_str (), std::ios::binary|std::ios::in);

PSTDOFSPICINPUT->SEEKG (0, Std::ios::end);

int nfilesize = PSTDOFSPICINPUT->TELLG ();

if (Npicfilelen = = 0)

{

return e_accessdenied;

}

char *pchpicfilebuf = NULL;

Try

{

Pchpicfilebuf = new Char[npicfilelen];

}

catch (Std::bad_alloc)

{

hr = E_FAIL;

}

if (FAILED (HR))

{

return HR;

}

PSTDOFSPICINPUT->SEEKG (0, Std::ios::beg);

Pstdofspicinput->read (Pchpicfilebuf, Npicfilelen);

if (Pstdofspicinput->bad ())

{

Pstdofspicinput->close ();

hr = E_FAIL;

}

Delete Pstdofspicinput;

if (FAILED (HR))

{

return HR;

}

Calculate the length of data to send.

std::string Stramultipartfirst = Cencodetool::ws2s (Strmultipartfirst);

std::string stramultipartinter = Cencodetool::ws2s (Strmultipartinter);

std::string stramultipartend = Cencodetool::ws2s (strmultipartend);

int csendbuflen = stramultipartfirst.size () + Npicfilelen + stramultipartinter.size () + stramultipartend.size ();

Allocate the buffer to temporary store, the data to send.

PCHAR pchsendbuf = new Char[csendbuflen];

memcpy (Pchsendbuf, Stramultipartfirst.c_str (), stramultipartfirst.size ());

memcpy (Pchsendbuf + stramultipartfirst.size (), (const char *) PCHPICFILEBUF, Npicfilelen);

memcpy (Pchsendbuf + stramultipartfirst.size () + Npicfilelen, Stramultipartinter.c_str (), stramultipartinter.size ());

memcpy (Pchsendbuf + stramultipartfirst.size () + Npicfilelen + stramultipartinter.size (), Stramultipartend.c_str (), Stramultipartend.size ());

//

Send the request data.

//

HttpSendRequest (m_hrequest, NULL, 0, (LPVOID) pchsendbuf, Csendbuflen)

Http://www.pc6.com/infoview/Article_50285.html

How to upload a file using the Multipart/form-data format (POST request, the data is placed in the request body, not in the request header, in the HTML protocol, with "\ r \ n" line, instead of "\ n")

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.