Uploads the local file to the specified server (HttpWebRequest method), through the file stream, with the file name, the form text fields and values that are uploaded together with the files.
1 ///<summary>2 ///uploading local files to the specified server (HttpWebRequest method)3 /// </summary>4 /// <param name= "Address" >files uploaded to the server</param>5 /// <param name= "Filenamepath" >local file to upload (full path)</param>6 /// <param name= "Savename" >name of file after uploading</param>7 /// <param name= "ProgressBar" >Upload progress bar</param>8 /// <returns>successfully returned 1, failed to return 0</returns>9 Private stringUploadrequest (stringUrlstringFilePath,stringfileName)Ten { One stringReturnValue =""; A //the file to upload -FileStream fs =NewFileStream (FilePath, FileMode.Open, FileAccess.Read); -BinaryReader r =NewBinaryReader (FS); the //time Stamp - stringStrboundary ="----------"+ DateTime.Now.Ticks.ToString ("x"); - byte[] boundarybytes = Encoding.ASCII.GetBytes ("\r\n--"+ Strboundary +"--\r\n"); - //Request header Information +StringBuilder SB =NewStringBuilder (); -Sb. Append ("--"); + sb. Append (strboundary); ASb. Append ("\ r \ n"); atSb. Append ("content-disposition:form-data; name=\ ""); -Sb. Append ("file"); -Sb. Append ("\ "; filename=\""); - sb. Append (fileName); -Sb. Append ("\""); -Sb. Append ("\ r \ n"); inSb. Append ("Content-type:"); -Sb. Append ("Application/octet-stream"); toSb. Append ("\ r \ n"); +Sb. Append ("\ r \ n"); - stringStrpostheader =sb. ToString (); the System.Console.Write (strpostheader); *System.Console.Write ("file contents ...."); $System.Console.Write ("\r\n--"+ Strboundary +"--\r\n");Panax Notoginseng byte[] Postheaderbytes =Encoding.UTF8.GetBytes (strpostheader); - //to create a HttpWebRequest object from a URI theHttpWebRequest Httpreq = (HttpWebRequest) webrequest.create (NewUri (URL)); +Httpreq.method ="POST"; A //do not use caching for sent data theHttpreq.allowwritestreambuffering =false; + //set time-out for response (300 seconds) -Httpreq.timeout =300000; $Httpreq.contenttype ="multipart/form-data; boundary="+strboundary; $ LongLength = fs. Length + postheaderbytes.length +boundarybytes.length; - LongFilelength =FS. Length; -httpreq.contentlength = length;//Request Content Length the Try - {Wuyi //5M per upload the intBufferlength =1024x768*1024x768*5; - byte[] buffer =New byte[bufferlength]; Wu //number of bytes uploaded - Longoffset =0; About //Start upload Time $DateTime StartTime =DateTime.Now; - intSize = R.read (buffer,0, bufferlength); -Stream Poststream =Httpreq.getrequeststream (); - //Send Request Header message APoststream.write (Postheaderbytes,0, postheaderbytes.length); + while(Size >0) the { -Poststream.write (Buffer,0, size); $Offset + =size; the theTimeSpan span = DateTime.Now-StartTime; the DoubleSecond =span. TotalSeconds; theLbltime.text ="Elapsed:"+ second. ToString ("F2") +"seconds"; - if(Second >0.001) in { theLblspeed.text ="Average Speed:"+ (Offset/1024x768/second). ToString ("0.00") +"kb/sec"; the } About Else the { theLblspeed.text ="connecting ..."; the } +Lblstate.text ="uploaded:"+ (OFFSET *100.0/length). ToString ("F2") +"%"; -Lblsize.text = (Offset/1048576.0). ToString ("F2") +"m/"+ (Filelength/1048576.0). ToString ("F2") +"M"; the BayiSize = R.read (buffer,0, bufferlength); the } the //Add a timestamp for the trailer -Poststream.write (Boundarybytes,0, boundarybytes.length); - poststream.close (); the //get the server-side response theWebResponse WebResponse =httpreq.getresponse (); theStream s =WebResponse.GetResponseStream (); theStreamReader sr =NewStreamReader (s); - //read messages returned by the server side theReturnValue =Sr. ReadLine (); the s.close (); the Sr. Close ();94 the } the Catch(Exception ex) the {98ReturnValue = ex. Message +Ex. StackTrace; About } - finally101 {102 FS. Close ();103 r.close ();104 } the returnreturnvalue;106}
Uploading local files to the specified server (HttpWebRequest method)