예제 #1
0
 void uploadClient_FileUploadCompleted(object sender, FileUploadCompletedEventArgs e)
 {
     this.OnFileUploadCompleted(e);
 }
예제 #2
0
        /// <summary>
        /// 当一个文件被上传记录FileUploadCompleted事件
        /// </summary>
        void client_FileUploadCompleted(object sender, FileUploadCompletedEventArgs e)
        {
            string log = string.Format("{0} 上传 {1} 到 {2}成功. 长度: {3}. ",
                DateTime.Now, e.LocalFile.FullName, e.ServerPath, e.LocalFile.Length);

            this.lstLog.Items.Add(log);
            this.lstLog.SelectedIndex = this.lstLog.Items.Count - 1;
        }
            /// <summary>
            /// Upload a single file directly.
            /// </summary>
            void UploadFile(FileInfo file, Uri serverPath)
            {
                if (file == null)
                {
                    throw new ArgumentNullException(" The file to upload is null. ");
                }

                Uri destPath = new Uri(serverPath, file.Name);

                // Create a request to upload the file.
                FtpWebRequest request = WebRequest.Create(destPath) as FtpWebRequest;

                request.Credentials = this.manager.Credentials;

                // Upload file.
                request.Method = WebRequestMethods.Ftp.UploadFile;

                FtpWebResponse response        = null;
                Stream         requestStream   = null;
                FileStream     localFileStream = null;

                try
                {
                    // Retrieve the response from the server.
                    response = request.GetResponse() as FtpWebResponse;

                    // Open the local file to read.
                    localFileStream = file.OpenRead();

                    // Retrieve the request stream.
                    requestStream = request.GetRequestStream();

                    int    bytesSize    = 0;
                    byte[] uploadBuffer = new byte[FTPUploadClient.BufferSize];

                    while (true)
                    {
                        // Read a buffer of local file.
                        bytesSize = localFileStream.Read(uploadBuffer, 0,
                                                         uploadBuffer.Length);

                        if (bytesSize == 0)
                        {
                            break;
                        }
                        else
                        {
                            // Write the buffer to the request stream.
                            requestStream.Write(uploadBuffer, 0, bytesSize);
                        }
                    }

                    var fileUploadCompletedEventArgs = new FileUploadCompletedEventArgs
                    {
                        LocalFile  = file,
                        ServerPath = destPath
                    };

                    this.OnFileUploadCompleted(fileUploadCompletedEventArgs);
                }
                catch (System.Net.WebException webEx)
                {
                    FtpWebResponse ftpResponse = webEx.Response as FtpWebResponse;

                    string msg = string.Format(
                        "There is an error while upload {0}. "
                        + " StatusCode: {1}  StatusDescription: {2} ",
                        file.FullName,
                        ftpResponse.StatusCode.ToString(),
                        ftpResponse.StatusDescription);
                    ApplicationException errorException
                        = new ApplicationException(msg, webEx);

                    // Fire the ErrorOccurred event with the error.
                    ErrorEventArgs e = new ErrorEventArgs
                    {
                        ErrorException = errorException
                    };

                    this.manager.OnErrorOccurred(e);
                }
                catch (Exception ex)
                {
                    string msg = string.Format(
                        "There is an error while upload {0}. "
                        + " See InnerException for detailed error. ",
                        file.FullName);
                    ApplicationException errorException
                        = new ApplicationException(msg, ex);

                    // Fire the ErrorOccurred event with the error.
                    ErrorEventArgs e = new ErrorEventArgs
                    {
                        ErrorException = errorException
                    };

                    this.manager.OnErrorOccurred(e);
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }

                    if (requestStream != null)
                    {
                        requestStream.Close();
                    }

                    if (localFileStream != null)
                    {
                        localFileStream.Close();
                    }
                }
            }
예제 #4
0
            /// <summary>
            /// 直接上传一个单独文件
            /// </summary>
            void UploadFile(FileInfo file, Uri serverPath)
            {
                if (file == null)
                {
                    throw new ArgumentNullException(" 上传文件为空. ");
                }

                Uri destPath = new Uri(serverPath, file.Name);

                //创建一个上传文件的请求
                FtpWebRequest request = WebRequest.Create(destPath) as FtpWebRequest;

                request.Credentials = this.manager.Credentials;

                // 上传文件.
                request.Method = WebRequestMethods.Ftp.UploadFile;

                FtpWebResponse response = null;
                Stream requestStream = null;
                FileStream localFileStream = null;

                try
                {

                    //从服务器检索响应
                    response = request.GetResponse() as FtpWebResponse;

                    //打开读取本地文件
                    localFileStream = file.OpenRead();

                    //检索要求流
                    requestStream = request.GetRequestStream();

                    int bytesSize = 0;
                    byte[] uploadBuffer = new byte[FTPUploadClient.BufferSize];

                    while (true)
                    {

                        //读取本地文件的缓冲
                        bytesSize = localFileStream.Read(uploadBuffer, 0,
                          uploadBuffer.Length);

                        if (bytesSize == 0)
                        {
                            break;
                        }
                        else
                        {

                            //在请求流中写缓冲。
                            requestStream.Write(uploadBuffer, 0, bytesSize);

                        }
                    }

                    var fileUploadCompletedEventArgs = new FileUploadCompletedEventArgs
                    {

                        LocalFile = file,
                        ServerPath = destPath
                    };

                    this.OnFileUploadCompleted(fileUploadCompletedEventArgs);

                }
                catch (System.Net.WebException webEx)
                {
                    FtpWebResponse ftpResponse = webEx.Response as FtpWebResponse;

                    string msg = string.Format(
                                       "当上传{0}有个错误. "
                                       + " StatusCode: {1}  StatusDescription: {2} ",
                                       file.FullName,
                                       ftpResponse.StatusCode.ToString(),
                                       ftpResponse.StatusDescription);
                    ApplicationException errorException
                        = new ApplicationException(msg, webEx);

                    //执行带错误的 ErrorOccurred事件
                    ErrorEventArgs e = new ErrorEventArgs
                    {
                        ErrorException = errorException
                    };

                    this.manager.OnErrorOccurred(e);
                }
                catch (Exception ex)
                {
                    string msg = string.Format(
                                       "当上传 {0}有个错误. "
                                       + " See InnerException for detailed error. ",
                                       file.FullName);
                    ApplicationException errorException
                        = new ApplicationException(msg, ex);

                    //激发ErrorOccurred事件
                    ErrorEventArgs e = new ErrorEventArgs
                    {
                        ErrorException = errorException
                    };

                    this.manager.OnErrorOccurred(e);
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }

                    if (requestStream != null)
                    {
                        requestStream.Close();
                    }

                    if (localFileStream != null)
                    {
                        localFileStream.Close();
                    }
                }
            }
예제 #5
0
 protected virtual void OnFileUploadCompleted(FileUploadCompletedEventArgs e)
 {
     if (FileUploadCompleted != null)
     {
         FileUploadCompleted(this, e);
     }
 }
예제 #6
0
            /// <summary>
            /// 直接上传一个单独文件
            /// </summary>
            void UploadFile(FileInfo file, Uri serverPath)
            {
                if (file == null)
                {
                    throw new ArgumentNullException(" 上传文件为空. ");
                }

                Uri destPath = new Uri(serverPath, file.Name);


                //创建一个上传文件的请求
                FtpWebRequest request = WebRequest.Create(destPath) as FtpWebRequest;

                request.Credentials = this.manager.Credentials;

                // 上传文件.
                request.Method = WebRequestMethods.Ftp.UploadFile;

                FtpWebResponse response        = null;
                Stream         requestStream   = null;
                FileStream     localFileStream = null;

                try
                {
                    //从服务器检索响应
                    response = request.GetResponse() as FtpWebResponse;

                    //打开读取本地文件
                    localFileStream = file.OpenRead();

                    //检索要求流
                    requestStream = request.GetRequestStream();

                    int    bytesSize    = 0;
                    byte[] uploadBuffer = new byte[FTPUploadClient.BufferSize];

                    while (true)
                    {
                        //读取本地文件的缓冲
                        bytesSize = localFileStream.Read(uploadBuffer, 0,
                                                         uploadBuffer.Length);

                        if (bytesSize == 0)
                        {
                            break;
                        }
                        else
                        {
                            //在请求流中写缓冲。
                            requestStream.Write(uploadBuffer, 0, bytesSize);
                        }
                    }

                    var fileUploadCompletedEventArgs = new FileUploadCompletedEventArgs
                    {
                        LocalFile  = file,
                        ServerPath = destPath
                    };

                    this.OnFileUploadCompleted(fileUploadCompletedEventArgs);
                }
                catch (System.Net.WebException webEx)
                {
                    FtpWebResponse ftpResponse = webEx.Response as FtpWebResponse;

                    string msg = string.Format(
                        "当上传{0}有个错误. "
                        + " StatusCode: {1}  StatusDescription: {2} ",
                        file.FullName,
                        ftpResponse.StatusCode.ToString(),
                        ftpResponse.StatusDescription);
                    ApplicationException errorException
                        = new ApplicationException(msg, webEx);

                    //执行带错误的 ErrorOccurred事件
                    ErrorEventArgs e = new ErrorEventArgs
                    {
                        ErrorException = errorException
                    };

                    this.manager.OnErrorOccurred(e);
                }
                catch (Exception ex)
                {
                    string msg = string.Format(
                        "当上传 {0}有个错误. "
                        + " See InnerException for detailed error. ",
                        file.FullName);
                    ApplicationException errorException
                        = new ApplicationException(msg, ex);

                    //激发ErrorOccurred事件
                    ErrorEventArgs e = new ErrorEventArgs
                    {
                        ErrorException = errorException
                    };

                    this.manager.OnErrorOccurred(e);
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }

                    if (requestStream != null)
                    {
                        requestStream.Close();
                    }

                    if (localFileStream != null)
                    {
                        localFileStream.Close();
                    }
                }
            }
예제 #7
0
 void uploadClient_FileUploadCompleted(object sender, FileUploadCompletedEventArgs e)
 {
     this.OnFileUploadCompleted(e);
 }
 /// <summary>
 /// Log the FileUploadCompleted event when a file was uploaded.
 /// </summary>
 void client_FileUploadCompleted(object sender, FileUploadCompletedEventArgs e)
 {
     this.Invoke(new EventHandler <FileUploadCompletedEventArgs>(
                     client_FileUploadCompletedHandler), sender, e);
 }