Symbian C ++ multipart/form-data)

Source: Internet
Author: User

The following code can be used to upload any file, image, text, video Etc .. as a binary file to a server.

The httpengine shoshould haveMhttpdatasupplierAs its parent class, as the following functions need to be implemented for the upload Process

 
Tbool getnextdatapart (tptrc8 & adatapart );
Void releasedata ();
Tint reset ();
Tint overalldatasize ();

The Content-Type for the post shocould be

 
_ Lit8 (kpostcontenttype, "multipart/form-data; boundary = aab03x ");

InMhfrunlThe caseEgotresponsebodydataShocould do this

Mhttpdatasupplier * datasupplier = atransaction. Response (). Body ();
Tptrc8 PTR;
Datasupplier-> getnextdatapart (PTR );
 
 
// Convert to 16-bit Descriptor
Hbufc * Buf = hbufc: newlc (PTR. Length ());
Buf-> des (). Copy (PTR );
 
// Append to ireeclipsebuffer
If (ireeclipsebuffer = NULL)
{
Ireeclipsebuffer = Buf-> allocl ();
}
Else
{
Ireeclipsebuffer = ireeclipsebuffer-> reallocl (ireeclipsebuffer-> length () + Buf-> length ());
Ireeclipsebuffer-> des (). append (* BUF );
}
 
// Release Buf
Cleanupstack: popanddestroy (BUF );
 
If (! Idataavailable)
{
Irunning = efalse;
Itransaction. Close ();
}
Else
{
Datasupplier-> releasedata ();
}

Post CodeGet the file you want to upload in binary format

Rfs afs;
User: leaveiferror (AFS. Connect ());
Cleanupclosepushl (AFS );
 
Rfile afile;
 
User: leaveiferror (afile. Open (AFS, ifilename, efile1_any ));
 
Tint asize;
User: leaveiferror (afile. Size (asize ));
 
Ipostdataimage = hbufc8: newl (asize );
Tptr8 aptr = ipostdataimage-> des ();
 
// Obviously we have to read in the data to the ipostdataimage (Ray)
Afile. Read (0, aptr, asize );
 
Afile. Close ();
AFS. Close ();
Cleanupstack: popanddestroy (& AFS );

UsingEfile1_anyEnsures that if the file can be opened by any other application. The file is readIpostdataimageAs a binary file.

Now create the upload format

_ Lit8 (kdatastart, "-- aab03x ");
_ Lit8 (kcrlf, "\ r \ n ");
_ Lit8 (kcontent, "content-Disposition: Form-data; name = 'userfile'; filename = '");
_ Lit8 (kfilecompletion ,"'");

_ Contents (kcontent2, "Content-Type: image/GIF ");
_ Contents (kcontent3, "content-transfer-encoding: Binary ");
_ Lit8 (kdataend, "-- aab03x --");

Ipostdata = hbufc8: newl (650 + aptr. Length ());


Tptr8 ipostdataptr = ipostdata-> des ();
Ipostdataptr. Zero ();

Ipostdataptr. append (kcrlf );
Ipostdataptr. append (kdatastart );
Ipostdataptr. append (kcrlf );
Ipostdataptr. append (kcontent );
Ipostdataptr. append (afname );
Ipostdataptr. append (kfilecompletion );
Ipostdataptr. append (kcrlf );
Ipostdataptr. append (kcontent2 );
Ipostdataptr. append (kcrlf );
Ipostdataptr. append (kcontent3 );
Ipostdataptr. append (kcrlf );
Ipostdataptr. append (kcrlf );
Ipostdataptr. append (aptr); // the file in binary
Ipostdataptr. append (kcrlf );
Ipostdataptr. append (kdataend );
Ipostdataptr. append (kcrlf );

Submit the transaction

Rhttpheaders HDR = itransaction. Request (). getheadercollection ();
Addheaderl (HDR, http: euseragent, kuseragent );
Addheaderl (HDR, http: eaccept, kaccept );
Addheaderl (HDR, http: econtenttype, kpostcontenttype );
 
Mhttpdatasupplier * datasupplier = this;
Itransaction. Request (). setbody (* datasupplier );
 
Itransaction. submitl ();

Implement datasupplier Functions

 
Tbool chttpexampleengine: getnextdatapart (tptrc8 & adatapart)
{
Tbool retval = efalse;
Adatapart. Set (ipostdata-> des ());
Retval = (adatapart. Length () = 0 );
Idataavailable = retval;
Return retval;
}
 
Tint chttpexampleengine: overalldatasize ()
{
If (ipostdata)
Return ipostdata-> length ();
Else
Return kerrnotfound;
}
 
Void chttpexampleengine: releasedata ()
{
If (idataavailable)
Itransaction. policynewrequestbodypartl ();
}

Any binary file can be uploaded with this upload code.

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.