Пример #1
0
        public static void UploadFile(string localFileToUpload, string targetUrl, string userName, string password, bool keepAlive)
        {
            FtpState state = new FtpState();

            state.FileName = localFileToUpload;


            StartUploadUsingFtpState(targetUrl, userName, password, keepAlive, state);
        }
Пример #2
0
        private static void EndGetResponseCallback(IAsyncResult result)
        {
            FtpState       state    = (FtpState)result.AsyncState;
            FtpWebResponse response = null;

            response = (FtpWebResponse)state.Request.EndGetResponse(result);
            response.Close();
            state.StatusDescription = response.StatusDescription;

            state.OperationComplete.Set();
        }
Пример #3
0
        private static void EndGetStreamCallback(IAsyncResult result)
        {
            FtpState state = (FtpState)result.AsyncState;

            Stream requestStream = null;

            Stream uploadStream = null;

            try
            {
                requestStream = state.Request.EndGetRequestStream(result);
                const int bufferLength = 2048;
                byte[]    buffer       = new byte[bufferLength];
                int       count        = 0;
                int       readBytes    = 0;

                if (!string.IsNullOrEmpty(state.TextToUpload))
                {
                    byte[] byteArray = Encoding.ASCII.GetBytes(state.TextToUpload);
                    uploadStream = new MemoryStream(byteArray);
                }
                else
                {
                    uploadStream = File.OpenRead(state.FileName);
                }

                do
                {
                    readBytes = uploadStream.Read(buffer, 0, bufferLength);
                    requestStream.Write(buffer, 0, readBytes);
                    count += readBytes;
                }while (readBytes != 0);

                requestStream.Close();

                state.Request.BeginGetResponse(
                    new AsyncCallback(EndGetResponseCallback),
                    state
                    );
            }
            catch (WebException webException)
            {
                state.Request.Abort();
                state.OperationComplete.Set();
                state.OperationException = webException;
            }
            finally
            {
                if (uploadStream != null)
                {
                    uploadStream.Close();
                }
            }
        }
Пример #4
0
        private static void StartUploadUsingFtpState(string targetUrl, string userName, string password, bool keepAlive, FtpState state)
        {
            ManualResetEvent waitObject;

            FtpWebRequest request;

            bool fileUploaded = false;

            do
            {
                try
                {
                    request = GetFtpWebRequest(targetUrl, userName, password, WebRequestMethods.Ftp.UploadFile, keepAlive);



                    waitObject    = state.OperationComplete;
                    state.Request = request;

                    var result = request.BeginGetRequestStream(
                        new AsyncCallback(EndGetStreamCallback),
                        state
                        );

                    waitObject.WaitOne();


                    var resultAsyncState = (FtpState)result.AsyncState;
                    if (resultAsyncState.OperationException != null)
                    {
                        throw resultAsyncState.OperationException;
                    }


                    fileUploaded = true;
//					state.Request.GetResponse();
                }
                catch (WebException e)
                {
                    if (e.ToString().Contains("550"))
                    {
                        fileUploaded = false;
//						CreateDirectory(userName, password, ReturnBaseDirectory(targetUrl), 2);
                    }
                    if (e.ToString().Contains("530"))
                    {
                        throw e;
                    }
                    else
                    {
                        throw e;
                    }
                }
                catch (Exception exception)
                {
                    int m = 3;
                }
            } while (!fileUploaded);
        }
Пример #5
0
		private static void StartUploadUsingFtpState(string targetUrl, string userName, string password, bool keepAlive, FtpState state)
		{
			ManualResetEvent waitObject;
			
			FtpWebRequest request;
			
			bool fileUploaded = false;
			do
			{
				try
				{

					
					request = GetFtpWebRequest(targetUrl, userName, password, WebRequestMethods.Ftp.UploadFile, keepAlive);



					waitObject = state.OperationComplete;
					state.Request = request;
					
					var result = request.BeginGetRequestStream(
						new AsyncCallback(EndGetStreamCallback),
						state
						);

					waitObject.WaitOne();


					var resultAsyncState = (FtpState)result.AsyncState;
					if(resultAsyncState.OperationException != null)
					{
						throw resultAsyncState.OperationException;
					}


					fileUploaded = true;
//					state.Request.GetResponse();

				}
				catch (WebException e)
				{
					if (e.ToString().Contains("550"))
					{
						fileUploaded = false;
//						CreateDirectory(userName, password, ReturnBaseDirectory(targetUrl), 2);
					}
					if(e.ToString ().Contains ("530"))
					{
						throw e;

					}
					else
					{
						throw e;
					}
				}
				catch(Exception exception)
				{
					int m = 3;
				}
			} while (!fileUploaded);
		}
Пример #6
0
		public static void UploadFile(string localFileToUpload, string targetUrl, string userName, string password, bool keepAlive)
		{
			
			FtpState state = new FtpState();
			
			state.FileName = localFileToUpload;
			
			
			StartUploadUsingFtpState(targetUrl, userName, password, keepAlive, state);
		}