Example #1
0
        /// <summary>
        ///当一个文件被下载时记录FileDownloadCompleted事件
        /// </summary>
        void client_FileDownloadCompleted(object sender, FileDownloadCompletedEventArgs e)
        {
            string log = string.Format("{0} 下载从 {1} 到 {2} 完成. 长度: {3}. ",
                DateTime.Now, e.ServerPath, e.LocalFile.FullName, e.LocalFile.Length);

            this.lstLog.Items.Add(log);
            this.lstLog.SelectedIndex = this.lstLog.Items.Count - 1;
        }
Example #2
0
 protected virtual void OnFileDownloadCompleted(FileDownloadCompletedEventArgs e)
 {
     if (FileDownloadCompleted != null)
     {
         FileDownloadCompleted(this, e);
     }
 }
Example #3
0
 void downloadClient_FileDownloadCompleted(object sender, FileDownloadCompletedEventArgs e)
 {
     this.OnFileDownloadCompleted(e);
 }
Example #4
0
            /// <summary>
            /// 下载一个单一的文件目录.
            /// </summary>
            void DownloadFile(FTPFileSystem file, string localPath)
            {
                if (file.IsDirectory)
                {
                    throw new ArgumentException(
                        "The FTPFileSystem to download is a directory in fact");
                }

                string destPath = localPath + "\\" + file.Name;

                //创建一个要下载的文件的请求.
                FtpWebRequest request = WebRequest.Create(file.Url) as FtpWebRequest;

                request.Credentials = this.manager.Credentials;

                // 下载文件.
                request.Method = WebRequestMethods.Ftp.DownloadFile;

                FtpWebResponse response = null;
                Stream responseStream = null;
                MemoryStream downloadCache = null;

                try
                {

                    //检索来自服务器的响应,并得到响应流.
                    response = request.GetResponse() as FtpWebResponse;

                    this.manager.OnNewMessageArrived(new NewMessageEventArg
                    {
                        NewMessage = response.StatusDescription
                    });

                    responseStream = response.GetResponseStream();

                    //内存中的缓存数据.
                    downloadCache = new MemoryStream(FTPDownloadClient.MaxCacheSize);
                    byte[] downloadBuffer = new byte[FTPDownloadClient.BufferSize];

                    int bytesSize = 0;
                    int cachedSize = 0;

                    // 下载这个文件直到下载完成.
                    while (true)
                    {

                        //从流中读取数据的缓冲区。
                        bytesSize = responseStream.Read(downloadBuffer, 0,
                            downloadBuffer.Length);

                        //如果这个缓存区是空的,或者下载完成,把在缓存中的数据写到本地文件中。
                       if (bytesSize == 0
                            || MaxCacheSize < cachedSize + bytesSize)
                        {
                            try
                            {
                                // 把在缓存中的数据写到本地文件中.
                                WriteCacheToFile(downloadCache, destPath, cachedSize);

                                //如果被暂停下载将停止正在下载的文件,取消或者完成.
                                if (bytesSize == 0)
                                {
                                    break;
                                }

                                // 重置缓存.
                                downloadCache.Seek(0, SeekOrigin.Begin);
                                cachedSize = 0;
                            }
                            catch (Exception ex)
                            {
                                string msg = string.Format(
                                    "There is an error while downloading {0}. "
                                    + " See InnerException for detailed error. ",
                                    file.Url);
                                ApplicationException errorException
                                    = new ApplicationException(msg, ex);

                                //这个错误给了DownloadCompleted事件
                                ErrorEventArgs e = new ErrorEventArgs
                                {
                                    ErrorException = errorException
                                };

                                this.manager.OnErrorOccurred(e);

                                return;
                            }

                        }

                        //把缓冲区的数据写到内存中的缓存。

                        downloadCache.Write(downloadBuffer, 0, bytesSize);
                        cachedSize += bytesSize;
                    }

                    var fileDownloadCompletedEventArgs = new FileDownloadCompletedEventArgs
                    {

                        LocalFile = new FileInfo(destPath),
                        ServerPath = file.Url
                    };

                    this.OnFileDownloadCompleted(fileDownloadCompletedEventArgs);
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }

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

                    if (downloadCache != null)
                    {
                        downloadCache.Close();
                    }
                }
            }
 void downloadClient_FileDownloadCompleted(object sender, FileDownloadCompletedEventArgs e)
 {
     this.OnFileDownloadCompleted(e);
 }
Example #6
0
 /// <summary>
 /// Log the FileDownloadCompleted event when a file was downloaded.
 /// </summary>
 void client_FileDownloadCompleted(object sender, FileDownloadCompletedEventArgs e)
 {
     this.Invoke(new EventHandler <FileDownloadCompletedEventArgs>(
                     client_FileDownloadCompletedHandler), sender, e);
 }
Example #7
0
            /// <summary>
            /// Download a single file directly.
            /// </summary>
            void DownloadFile(FTPFileSystem file, string localPath)
            {
                if (file.IsDirectory)
                {
                    throw new ArgumentException(
                              "The FTPFileSystem to download is a directory in fact");
                }

                string destPath = localPath + "\\" + file.Name;

                // Create a request to the file to be  downloaded.
                FtpWebRequest request = WebRequest.Create(file.Url) as FtpWebRequest;

                request.Credentials = this.manager.Credentials;

                // Download file.
                request.Method = WebRequestMethods.Ftp.DownloadFile;

                FtpWebResponse response       = null;
                Stream         responseStream = null;
                MemoryStream   downloadCache  = null;


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

                    this.manager.OnNewMessageArrived(new NewMessageEventArg
                    {
                        NewMessage = response.StatusDescription
                    });

                    responseStream = response.GetResponseStream();

                    // Cache data in memory.
                    downloadCache = new MemoryStream(FTPDownloadClient.MaxCacheSize);
                    byte[] downloadBuffer = new byte[FTPDownloadClient.BufferSize];

                    int bytesSize  = 0;
                    int cachedSize = 0;

                    // Download the file until the download is completed.
                    while (true)
                    {
                        // Read a buffer of data from the stream.
                        bytesSize = responseStream.Read(downloadBuffer, 0,
                                                        downloadBuffer.Length);

                        // If the cache is full, or the download is completed, write
                        // the data in cache to local file.
                        if (bytesSize == 0 ||
                            MaxCacheSize < cachedSize + bytesSize)
                        {
                            try
                            {
                                // Write the data in cache to local file.
                                WriteCacheToFile(downloadCache, destPath, cachedSize);

                                // Stop downloading the file if the download is paused,
                                // canceled or completed.
                                if (bytesSize == 0)
                                {
                                    break;
                                }

                                // Reset cache.
                                downloadCache.Seek(0, SeekOrigin.Begin);
                                cachedSize = 0;
                            }
                            catch (Exception ex)
                            {
                                string msg = string.Format(
                                    "There is an error while downloading {0}. "
                                    + " See InnerException for detailed error. ",
                                    file.Url);
                                ApplicationException errorException
                                    = new ApplicationException(msg, ex);

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

                                this.manager.OnErrorOccurred(e);

                                return;
                            }
                        }

                        // Write the data from the buffer to the cache in memory.
                        downloadCache.Write(downloadBuffer, 0, bytesSize);
                        cachedSize += bytesSize;
                    }

                    var fileDownloadCompletedEventArgs = new FileDownloadCompletedEventArgs
                    {
                        LocalFile  = new FileInfo(destPath),
                        ServerPath = file.Url
                    };

                    this.OnFileDownloadCompleted(fileDownloadCompletedEventArgs);
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }

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

                    if (downloadCache != null)
                    {
                        downloadCache.Close();
                    }
                }
            }
Example #8
0
            /// <summary>
            /// 下载一个单一的文件目录.
            /// </summary>
            void DownloadFile(FTPFileSystem file, string localPath)
            {
                if (file.IsDirectory)
                {
                    throw new ArgumentException(
                              "The FTPFileSystem to download is a directory in fact");
                }

                string destPath = localPath + "\\" + file.Name;

                //创建一个要下载的文件的请求.
                FtpWebRequest request = WebRequest.Create(file.Url) as FtpWebRequest;

                request.Credentials = this.manager.Credentials;

                // 下载文件.
                request.Method = WebRequestMethods.Ftp.DownloadFile;

                FtpWebResponse response       = null;
                Stream         responseStream = null;
                MemoryStream   downloadCache  = null;


                try
                {
                    //检索来自服务器的响应,并得到响应流.
                    response = request.GetResponse() as FtpWebResponse;

                    this.manager.OnNewMessageArrived(new NewMessageEventArg
                    {
                        NewMessage = response.StatusDescription
                    });

                    responseStream = response.GetResponseStream();

                    //内存中的缓存数据.
                    downloadCache = new MemoryStream(FTPDownloadClient.MaxCacheSize);
                    byte[] downloadBuffer = new byte[FTPDownloadClient.BufferSize];

                    int bytesSize  = 0;
                    int cachedSize = 0;

                    // 下载这个文件直到下载完成.
                    while (true)
                    {
                        //从流中读取数据的缓冲区。
                        bytesSize = responseStream.Read(downloadBuffer, 0,
                                                        downloadBuffer.Length);

                        //如果这个缓存区是空的,或者下载完成,把在缓存中的数据写到本地文件中。
                        if (bytesSize == 0 ||
                            MaxCacheSize < cachedSize + bytesSize)
                        {
                            try
                            {
                                // 把在缓存中的数据写到本地文件中.
                                WriteCacheToFile(downloadCache, destPath, cachedSize);

                                //如果被暂停下载将停止正在下载的文件,取消或者完成.
                                if (bytesSize == 0)
                                {
                                    break;
                                }

                                // 重置缓存.
                                downloadCache.Seek(0, SeekOrigin.Begin);
                                cachedSize = 0;
                            }
                            catch (Exception ex)
                            {
                                string msg = string.Format(
                                    "There is an error while downloading {0}. "
                                    + " See InnerException for detailed error. ",
                                    file.Url);
                                ApplicationException errorException
                                    = new ApplicationException(msg, ex);

                                //这个错误给了DownloadCompleted事件
                                ErrorEventArgs e = new ErrorEventArgs
                                {
                                    ErrorException = errorException
                                };

                                this.manager.OnErrorOccurred(e);

                                return;
                            }
                        }

                        //把缓冲区的数据写到内存中的缓存。

                        downloadCache.Write(downloadBuffer, 0, bytesSize);
                        cachedSize += bytesSize;
                    }

                    var fileDownloadCompletedEventArgs = new FileDownloadCompletedEventArgs
                    {
                        LocalFile  = new FileInfo(destPath),
                        ServerPath = file.Url
                    };

                    this.OnFileDownloadCompleted(fileDownloadCompletedEventArgs);
                }
                finally
                {
                    if (response != null)
                    {
                        response.Close();
                    }

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

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