C ++ builder writes winform to upload files to the Web server

Source: Internet
Author: User

To meet project requirements, you need to upload a winform file to the Web server. In this case, winform is written in C ++ builder2010 and the Web Service is written in C # on the Web server. After a long time, you can finally complete the process, the Code is as follows:

Winform:

Unicodestring templatesavepath = changefileext (extractfilepath (Application-> exename), "tmp_1.doc ");
Tidmultipartformdatastream * stream = new tidmultipartformdatastream ();
Tidhttp * HTTP = new tidhttp (null );
Try {
Stream-> addformfield ("fileupload1", "file"); // the file name must be the same as the input name.
Stream-> AddFile ("fileupload1", templatesavepath, "multipart/form-Data ");
Stream-> position = 0;
Stream-> addformfield ("_ viewstate", "/logs/kpsctjymhck1_ipw = ");
Stream-> addformfield ("_ eventvalidation", "/wewagljnm/wagkm54rgbpzhfrvylwbnyjjvucw6bwmjejqb ");
Stream-> addformfield ("button1", "button ");
Idantifreeze1-> onlywhenidle = false;
HTTP-> request-> contenttype = "multipart/form-Data ";
Unicodestring result = http-> post ("http: // localhost: 1723/upload_tmp/default. aspx", stream );
If (result = "OK "){
// Messagedlg ("File Uploaded successfully! ", Mtinformation, mbok, 0 );
} Else {
// Messagedlg ("File Upload Failed! ", Mtinformation, mbok, 0 );
}
}__ Finally {
Delete stream;
Delete HTTP;
}

The client needs to add several Asp.net-specific domains __viewstate and _ eventvalidation. Their values can be copied on the Asp.net page, and then the fields of the button field and the fileupload component can be added.

 

Web Server:

Default. aspx

Form ID = "form1" runat = "server">
<Div>
& Nbsp; <asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/> </div>
</Form>

Default. aspx. CS

Protected void button#click (Object sender, eventargs E)
{
Try
{
If (this. fileupload1.filebytes. length> 0)
{
String savepath = server. mappath ("~ /"+ Path. getfilename (fileupload1.filename ));
For (INT I = 0; I <this. Request. Files. Count; ++ I)
{
This. Request. Files [I]. saveas (savepath );
}
Response. Clear ();
Response. Write ("OK ");
}
}
Catch
{
Response. Write ("no ");
}
}

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.