Пример #1
0
        private void UploadFileAsyncWC(string uploadPath, string localPath, AsyncCompletedEventHandler completed)
        {
            Uri       ftpUri = this.Connection.GetFtpUri(uploadPath);
            WebClient wc     = null;

            using (wc = new WebClient())
            {
                this._uploadClientList.Add(wc);

                wc.Credentials = this.Connection.GetCredential();

                var OnUploadProgressChanged = UploadProgressChanged;
                if (OnUploadProgressChanged != null)
                {
                    wc.UploadProgressChanged += (s, e) =>
                    {
                        var args = e.UserState as FtpUploadProgressChangedEventArgs;
                        args.BytesSent = e.BytesSent;
                        OnUploadProgressChanged(this, args);
                    };
                }

                wc.UploadFileCompleted += (s, e) =>
                {
                    this._uploadClientList.Remove(s);

                    if (completed != null)
                    {
                        completed(this, e);
                    }
                    else
                    {
                        var OnUploadFileAsyncCompleted = UploadFileAsyncCompleted;
                        if (OnUploadFileAsyncCompleted != null)
                        {
                            OnUploadFileAsyncCompleted(this, new FtpAsyncCompletedEventArgs(e.Error, e.Cancelled));
                        }
                    }
                };

                try
                {
                    FileInfo fi       = new FileInfo(localPath);
                    long     fileSize = fi.Length;
                    var      args     = new FtpUploadProgressChangedEventArgs(0, fileSize);
                    wc.UploadFileAsync(ftpUri, null, localPath, args);
                }
                catch (Exception exception)
                {
                    this._uploadClientList.Remove(wc);
                    throw new FtpException(exception.Message, exception);
                }
            }
        }