WinForm HttpWebRequest Uploading files

Source: Internet
Author: User

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

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.