예제 #1
0
        public void bgworkerStartDownload_DoWorkHandler(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bgworkerStartDownload = sender as BackgroundWorker;
            Dictionary<string, string> fileInfo = (Dictionary<string, string>)e.Argument;
            Dictionary<string, string> asyncResult = new Dictionary<string, string>();
            asyncResult.Add("IsCompleted", "false");
            asyncResult.Add("FileId", fileInfo["FileId"]);
            asyncResult.Add("LocalFilePath", "");

            // Asynchronous FTP Download
            Chilkat.Ftp2 ftp = new Chilkat.Ftp2();

            bool success;

            success = ftp.UnlockComponent(GlobalHelper.ComponentCode);
            if (success != true)
            {
                MessageBox.Show(ftp.LastErrorText);
                return;
            }

            ftp.Hostname = GlobalHelper.FtpHost;
            ftp.Username = GlobalHelper.FtpUsername;
            ftp.Password = GlobalHelper.FtpPasswrod;
            // Resume download
            ftp.RestartNext = true;

            // Connect and login to the FTP server.
            success = ftp.Connect();
            if (success != true)
            {
                MessageBox.Show(ftp.LastErrorText);
                return;
            }

            string localFilename = String.Format(@"{0}\{1}",fileInfo["LocalFilePath"], fileInfo["LocalFileName"]);
            asyncResult["LocalFilePath"] = localFilename;
            string remoteFilename = fileInfo["RemoteFilePath"];
            long remoteFilesize = Convert.ToInt64(fileInfo["RemoteFileSize"]);

            success = ftp.AsyncGetFileStart(remoteFilename, localFilename);
            if (success != true)
            {
                MessageBox.Show(ftp.LastErrorText);
                return;
            }

            while (ftp.AsyncFinished != true)
            {
                if (_cancelList.Contains(fileInfo["FileId"]))
                {
                    ftp.AsyncAbort();
                    break;
                }

                if (File.Exists(localFilename))
                {
                    FileInfo localFile = new FileInfo(localFilename);
                    double percentage = ((double)localFile.Length / (double)remoteFilesize) * 100;
                    bgworkerStartDownload.ReportProgress((int)percentage);
                }

                // Sleep 0.5 second.
                ftp.SleepMs(500);
            }

            bool downloadSuccess = false;
            // Did the download succeed?
            if (ftp.AsyncSuccess == true)
            {
                downloadSuccess = true;
                bgworkerStartDownload.ReportProgress(100);
                asyncResult["IsCompleted"] = "true";
            }
            else
            {

            }

            ftp.Disconnect();
            ftp.Dispose();

            if (downloadSuccess)
            {
                File.Move(localFilename, localFilename.Replace(GlobalHelper.TempDownloadFileExt, String.Empty));
            }

            e.Result = asyncResult;
        }