WinForm Click button to upload:
stringFilePath ="E:\\test.rar"; stringFileName ="Test.rar"; stringPostURL ="http://localhost:5995/Default.aspx"; //Boundary character varBoundary ="---------------"+ DateTime.Now.Ticks.ToString ("x"); varBeginboundary = Encoding.ASCII.GetBytes ("--"+ Boundary +"\ r \ n"); varFileStream =NewFileStream (FilePath, FileMode.Open, FileAccess.Read); //the last Terminator varEndboundary = Encoding.ASCII.GetBytes ("--"+ Boundary +"--\r\n"); //File parameter Header Const stringFilepartheader ="Content-disposition:form-data, name=\ "{0}\"; filename=\ "{1}\" \ r \ n"+"content-type:application/octet-stream\r\n\r\n"; varFileheader =string. Format (Filepartheader,"file", FileName); varFileheaderbytes =Encoding.UTF8.GetBytes (Fileheader); //Start spelling Data varMemstream =NewMemoryStream (); Memstream.write (Beginboundary,0, beginboundary.length); //File DataMemstream.write (Fileheaderbytes,0, fileheaderbytes.length); varBuffer =New byte[1024x768]; intBytesread;//=0 while(Bytesread = filestream.read (buffer,0, buffer. Length))! =0) {memstream.write (buffer,0, Bytesread); } filestream.close (); //Key-value Data varStringkeyheader ="\r\n--"+ Boundary +"\r\ncontent-disposition:form-data; name=\ "{0}\""+"\r\n\r\n{1}\r\n"; Dictionary<string,string> stringdict =Newdictionary<string,string>(); Stringdict.add ("Len"," -"); Stringdict.add ("wid"," -"); foreach(byte[] Formitembytesinch from stringKeyinchStringdict.keysSelect string. Format (Stringkeyheader, Key, Stringdict[key]) into Formitem SelectEncoding.UTF8.GetBytes (Formitem)) {Memstream.write (formitembytes,0, Formitembytes. Length); } //writes the last end of the bounding characterMemstream.write (Endboundary,0, endboundary.length); //Daoteng to Tempbuffer? Memstream.position =0; varTempbuffer =New byte[Memstream.length]; Memstream.read (Tempbuffer,0, tempbuffer.length); Memstream.close (); //create WebRequest and set properties varWebRequest =(HttpWebRequest) webrequest.create (PostURL); Webrequest.method="POST"; Webrequest.timeout=100000; Webrequest.contenttype="multipart/form-data; boundary="+boundary; Webrequest.contentlength=tempbuffer.length; varRequeststream =Webrequest.getrequeststream (); requestStream.Write (Tempbuffer,0, tempbuffer.length); Requeststream.close (); varHttpWebResponse =(HttpWebResponse) webrequest.getresponse (); stringresponsecontent; using(varHttpstreamreader =NewStreamReader (Httpwebresponse.getresponsestream (), Encoding.GetEncoding ("Utf-8")) {responsecontent=Httpstreamreader.readtoend (); } httpwebresponse.close (); Webrequest.abort ();
Web site Default.aspx page:
Response.ContentType = text/plain Span style= "COLOR: #800000" > " ; if (Request.Files.Count = = 0 " no file " ); return ; } httppostedfile f1 = Request.files[0 ]; F1. SaveAs ( " e:\\test-upload.rar Span style= "COLOR: #800000" "");
String strpars = "";
foreach (var key in Request.Form.AllKeys)
{
String val = Request[key];
Strpars + = "[" + Key + ":" + val + "]";
}
Image. Dispose ();
Response.Write ("OK Get File:" + F1. FileName + "Pars:" + strpars);
WinForm HttpWebRequest Uploading files