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 ");
}
}