Ejemplo n.º 1
0
        /// <summary>
        /// Uploads a file from to the FTP server
        /// </summary>
        /// <param name="localFilePath">The path of the file to upload</param>
        /// <param name="remoteFileName">The name of the file on remote server</param>
        public virtual void PutFile(string localFilePath, string remoteFileName)
        {
            if (string.IsNullOrEmpty(this.RemoteHost))
            {
                throw new IOException("Remote host is undefined.");
            }

            ManualResetEvent waitObject;
            FtpState         state = new FtpState();
            string           remoteFileUri;
            string           remoteHost = _RemoteHost.Trim();

            if (!remoteHost.StartsWith("ftp://"))
            {
                remoteHost = "ftp://" + remoteHost;
            }

            if (remoteHost.EndsWith("/"))
            {
                remoteFileUri = remoteHost + remoteFileName;
            }
            else
            {
                remoteFileUri = remoteHost + "/" + remoteFileName;
            }
            Uri           target = new Uri(remoteFileUri);
            FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(target);

            ftpReq.UseBinary   = this.TransferType == FtpTransferType.BINARY;
            ftpReq.Credentials = new NetworkCredential(this.UserName, this.Password);
            ftpReq.Method      = WebRequestMethods.Ftp.UploadFile;

            // Store the request in the object that we pass into the
            // asynchronous operations.
            state.Request  = ftpReq;
            state.FileName = localFilePath;

            // Get the event to wait on.
            waitObject = state.OperationComplete;

            // Asynchronously get the stream for the file contents.
            ftpReq.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;
            }
        }
Ejemplo n.º 2
0
        // The EndGetResponseCallback method
        // completes a call to BeginGetResponse.
        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)
            {
                state.OperationException = e;
                state.OperationComplete.Set();
            }
        }
Ejemplo n.º 3
0
        private static void EndGetStreamCallback(IAsyncResult ar)
        {
            FtpState state = (FtpState)ar.AsyncState;

            Stream requestStream = null;

            // End the asynchronous call to get the request stream.
            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);

                // 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;
            }
        }