The following are the upload methods:
// <Summary> // upload a local file to the specified server (HttpWebRequest method) /// </summary> /// <param name = "address"> the server to which the file is uploaded </param> /// <param name = "fileNamePath"> to upload local file (full path) </param> /// <param name = "saveName"> name of the uploaded file </param> /// <param name = "progressBar"> upload progress bar </param >/// <returns> 1 is returned successfully, 0 </returns> private int Upload_Request (string address, string fileNamePath, string saveName, ProgressBar progressBar) {int retu RnValue = 0; // file to be uploaded FileStream fs = new FileStream (fileNamePath, FileMode. open, FileAccess. read); BinaryReader r = new BinaryReader (fs); // timestamp string strBoundary = "----------" + DateTime. now. ticks. toString ("x"); byte [] boundaryBytes = Encoding. ASCII. getBytes ("\ r \ n --" + strBoundary + "\ r \ n"); // request header information StringBuilder sb = new StringBuilder (); sb. append ("--"); sb. append (strBoundary); sb. append ("\ r \ N "); sb. append ("Content-Disposition: form-data; name = \" "); sb. append ("file"); sb. append ("\"; filename = \ ""); sb. append (saveName); sb. append ("\" "); sb. append ("\ r \ n"); sb. append ("Content-Type:"); sb. append ("application/octet-stream"); sb. append ("\ r \ n"); sb. append ("\ r \ n"); string strPostHeader = sb. toString (); byte [] postHeaderBytes = Encoding. UTF8.GetBytes (strPostHeader); // create an HttpWebRequest object based on the uri HttpWebRequest httpReq = (HttpWebRequest) WebRequest. create (new Uri (address); httpReq. method = "POST"; // do not use the httpReq cache for sent data. allowWriteStreamBuffering = false; // sets the response timeout (300 seconds) httpReq. timeout = 300000; httpReq. contentType = "multipart/form-data; boundary =" + strBoundary; long length = fs. length + postHeaderBytes. length + boundaryBytes. length; long fileLength = fs. length; httpReq. contentLength = Length; try {progressBar. maximum = int. maxValue; progressBar. minimum = 0; progressBar. value = 0; // 4 k int bufferLength = 4096 for each upload; byte [] buffer = new byte [bufferLength]; // Number of uploaded bytes long offset = 0; // start upload time DateTime startTime = DateTime. now; int size = r. read (buffer, 0, bufferLength); Stream postStream = httpReq. getRequestStream (); // send the request header message postStream. write (postHeaderBytes, 0, postHeaderByte S. length); while (size> 0) {postStream. write (buffer, 0, size); offset + = size; progressBar. value = (int) (offset * (int. maxValue/length); TimeSpan span = DateTime. now-startTime; double second = span. totalSeconds; lblTime. text = "in use:" + second. toString ("F2") + "seconds"; if (second> 0.001) {lblSpeed. text = "average speed:" + (offset/1024/second ). toString ("0.00") + "KB/second";} else {lblSpeed. text =" Connecting... ";} LblState. text = "uploaded:" + (offset * 100.0/length ). toString ("F2") + "%"; lblSize. text = (offset/1048576.0 ). toString ("F2") + "M/" + (fileLength/1048576.0 ). toString ("F2") + "M"; Application. doEvents (); size = r. read (buffer, 0, bufferLength);} // Add the end timestamp postStream. write (boundaryBytes, 0, boundaryBytes. length); postStream. close (); // obtain the server response WebResponse webRespon = httpReq. getResponse (); Stream s = webRespon. getResponseStream (); StreamReader sr = new StreamReader (s); // read the message String sReturnString = sr returned by the server. readLine (); s. close (); sr. close (); if (sReturnString = "Success") {returnValue = 1;} else if (sReturnString = "Error") {returnValue = 0 ;}} catch (Exception ex) {returnValue = 0;} finally {fs. close (); r. close () ;}return returnValue ;}