コード例 #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)
            {
                state.Request.Abort();
                state.OperationComplete.Set();
                state.OperationException = new Exception();
            }
            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
                {
                    fileUploaded = true;

                    request       = GetFtpWebRequest(targetUrl, userName, password, WebRequestMethods.Ftp.UploadFile, keepAlive);
                    waitObject    = state.OperationComplete;
                    state.Request = request;

                    request.BeginGetRequestStream(
                        new AsyncCallback(EndGetStreamCallback),
                        state
                        );
                    state.Request.GetResponse();
                    waitObject.WaitOne();
                }
                catch (WebException e)
                {
                    if (e.ToString().Contains("550"))
                    {
                        fileUploaded = false;
                        CreateDirectory(userName, password, ReturnBaseDirectory(targetUrl), 2);
                    }
                    else
                    {
                        throw e;
                    }
                }
            } while (!fileUploaded);
        }