Exemple #1
0
        private void DownloadFileAsyncWC(string downloadPath, string localPath, AsyncCompletedEventHandler completed)
        {
            Uri       ftpUri = this.Connection.GetFtpUri(downloadPath);
            WebClient wc     = null;

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

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

                var OnDownloadProgressChanged = DownloadProgressChanged;
                if (OnDownloadProgressChanged != null)
                {
                    wc.DownloadProgressChanged += (s, e) =>
                    {
                        var args = e.UserState as FtpDownloadProgressChangedEventArgs;
                        args.BytesReceived = e.BytesReceived;
                        OnDownloadProgressChanged(this, args);
                    };
                }

                wc.DownloadFileCompleted += (s, e) =>
                {
                    this._downloadClientList.Remove(s);

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

                try
                {
                    long fileSize = GetFileSize(downloadPath);
                    var  args     = new FtpDownloadProgressChangedEventArgs(0, fileSize);
                    wc.DownloadFileAsync(ftpUri, localPath, args);
                }
                catch (Exception exception)
                {
                    this._downloadClientList.Remove(wc);
                    throw new FtpException(exception.Message, exception);
                }
            }
        }
Exemple #2
0
        private void DownloadFileAsync(string downloadPath, string localPath, AsyncCompletedEventHandler completed)
        {
            BackgroundWorker downloadWorker = new BackgroundWorker();

            downloadWorker.WorkerReportsProgress      = true;
            downloadWorker.WorkerSupportsCancellation = true;

            this._downloadClientList.Add(downloadWorker);
            var OnDownloadProgressChanged = DownloadProgressChanged;

            downloadWorker.DoWork += (s, e) =>
            {
                FtpWebRequest request = this.Connection.GetRequest(downloadPath, WebRequestMethods.Ftp.DownloadFile);
                request.UseBinary = true;

                using (var response = this.Connection.GetResponse(request))
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (FileStream fs = new FileStream(localPath, FileMode.Create, FileAccess.Write))
                        {
                            byte[] buffer    = new byte[DOWNLOAD_BYTE];
                            int    readBytes = 0;

                            try
                            {
                                long totalBytes = GetFileSize(downloadPath);
                                var  args       = new FtpDownloadProgressChangedEventArgs(readBytes, totalBytes);

                                while ((readBytes = stream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    if (downloadWorker.CancellationPending)
                                    {
                                        e.Cancel = true;
                                        break;
                                    }

                                    fs.Write(buffer, 0, readBytes);

                                    if (OnDownloadProgressChanged != null)
                                    {
                                        args.BytesReceived = fs.Length;
                                        downloadWorker.ReportProgress(0, args);
                                    }
                                }

                                fs.Close();
                            }
                            catch
                            {
                                fs.Close();
                            }
                        }

                        stream.Close();
                    }

                    response.Close();
                }
            };

            if (OnDownloadProgressChanged != null)
            {
                downloadWorker.ProgressChanged += (s, e) =>
                {
                    var args = e.UserState as FtpDownloadProgressChangedEventArgs;
                    OnDownloadProgressChanged(this, args);
                };
            }

            downloadWorker.RunWorkerCompleted += (s, e) =>
            {
                this._downloadClientList.Remove(s);
                var args = e;

                if (!e.Cancelled && e.Result is Exception)
                {
                    var exception = e.Result as Exception;
                    args = new RunWorkerCompletedEventArgs(e.Result, exception, e.Cancelled);
                }

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

            try
            {
                downloadWorker.RunWorkerAsync();
            }
            catch (Exception exception)
            {
                this._downloadClientList.Remove(downloadWorker);
                throw new FtpException(exception.Message, exception);
            }
        }