public void UpLoad(object obj) { object[] objs = (object[])obj; List <string> uploadfilePath = objs[3] as List <string>; for (int i = 0; i < uploadfilePath.Count(); i++) { try { FtpState state = new FtpState(); state.FilePath = uploadfilePath[i]; ManualResetEvent waitObject; FileInfo fileInf = new FileInfo(state.FilePath); string fileName = fileInf.Name; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + objs[0] + "/zdsoft/bill_upload/" + fileInf.Name)); request.Method = WebRequestMethods.Ftp.UploadFile; request.Timeout = 60 * 60 * 1000; request.KeepAlive = false; request.Credentials = new NetworkCredential(objs[1].ToString(), objs[2].ToString()); request.Proxy = null; request.KeepAlive = false; request.ContentLength = fileInf.Length; state.Request = request; state.FileName = state.FilePath; // Get the event to wait on. waitObject = state.OperationComplete; // Asynchronously get the stream for the file contents. IAsyncResult ir = request.BeginGetRequestStream(new AsyncCallback(EndGetStreamCallback), state); // Block the current thread until all operations are complete. waitObject.WaitOne(); // The operations either completed or threw an exception. if (state.OperationException != null) { throw state.OperationException; } else { Console.WriteLine("The operation completed - {0}", state.StatusDescription); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
private void EndGetStreamCallback(IAsyncResult ar) { FtpState state = (FtpState)ar.AsyncState; Stream requestStream = null; try { requestStream = state.Request.EndGetRequestStream(ar); // Copy the file contents to the request stream. const int bufferLength = 2048; byte[] buffer = new byte[bufferLength]; int count = 0; int readBytes = 0; FileStream stream = File.OpenRead(state.FileName); do { readBytes = stream.Read(buffer, 0, bufferLength); requestStream.Write(buffer, 0, readBytes); count += readBytes; }while (readBytes != 0); stream.Close(); var fileName = state.FilePath.Split('.'); File.Move(state.FilePath, fileName[0] + "-success." + fileName[1]); //File.Delete(state.FilePath); // IMPORTANT: Close the request stream before sending the request. requestStream.Close(); // Asynchronously get the response to the upload request. IAsyncResult ir = state.Request.BeginGetResponse(new AsyncCallback(EndGetResponseCallback), state); } // Return exceptions to the main application thread. catch (Exception e) { state.OperationException = e; state.OperationComplete.Set(); return; } }
// The EndGetResponseCallback method // completes a call to BeginGetResponse. private void EndGetResponseCallback(IAsyncResult ar) { FtpState state = (FtpState)ar.AsyncState; FtpWebResponse response = null; try { response = (FtpWebResponse)state.Request.EndGetResponse(ar); response.Close(); state.StatusDescription = response.StatusDescription; // Signal the main application thread that // the operation is complete. var ir = state.OperationComplete.Set(); } // Return exceptions to the main application thread. catch (Exception e) { Console.WriteLine("Error getting response."); state.OperationException = e; state.OperationComplete.Set(); } }