public static void UploadFileAsync(string uriString, string fileName, string userName, string password) { ManualResetEvent waitObject; Uri uri = new Uri(uriString); FtpState ftpState = new FtpState(); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); request.Method = WebRequestMethods.File.UploadFile; request.Credentials = new NetworkCredential(userName, password); ftpState.Request = request; ftpState.FileName = fileName; waitObject = ftpState.OperationComplete; request.BeginGetRequestStream(new AsyncCallback(EndGetStreamCallback), ftpState); // Block the current thread until all operations are complete. waitObject.WaitOne(); if (ftpState.OperationException != null) { throw ftpState.OperationException; } else { Console.WriteLine("The operation completed - {0}", ftpState.StatusDescription); } }
private static 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. state.OperationComplete.Set(); } // Return exceptions to the main application thread. catch (Exception e) { Console.WriteLine("Error getting response."); state.OperationException = e; state.OperationComplete.Set(); } }
private static 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); Console.WriteLine("Writing {0} bytes to the stream.", count); // IMPORTANT: Close the request stream before sending the request. requestStream.Close(); // Asynchronously get the response to the upload request. state.Request.BeginGetResponse( new AsyncCallback(EndGetResponseCallback), state ); } // Return exceptions to the main application thread. catch (Exception e) { Console.WriteLine("Could not get the request stream."); state.OperationException = e; state.OperationComplete.Set(); return; } }