public void bgworkerStartUpload_DoWorkHandler(object sender, DoWorkEventArgs e) { ApiHelper api = new ApiHelper(); BackgroundWorker bgworkerStartUpload = 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("RemoteFilePath", fileInfo["RemoteFilePath"]); // Asynchronous FTP Upload 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 upload ftp.RestartNext = true; // Connect and login to the FTP server. success = ftp.Connect(); if (success != true) { MessageBox.Show(ftp.LastErrorText); return; } string localFilename = fileInfo["LocalFilePath"]; string remoteFilename = fileInfo["RemoteFilePath"]; long localFilesize = Convert.ToInt64(fileInfo["LocalFileSize"]); success = ftp.AsyncPutFileStart(localFilename, remoteFilename); if (success != true) { MessageBox.Show(ftp.LastErrorText); return; } while (ftp.AsyncFinished != true) { if (_cancelList.Contains(fileInfo["FileId"])) { ftp.AsyncAbort(); break; } if (api.CheckPath(remoteFilename)) { long remoteFilesize = api.GetFileSize(remoteFilename); double percentage = ((double)remoteFilesize / (double)localFilesize) * 100; bgworkerStartUpload.ReportProgress((int)percentage); } // Sleep 0.5 second. ftp.SleepMs(500); } bool uploadSuccess = false; // Did the upload succeed? if (ftp.AsyncSuccess == true) { uploadSuccess = true; bgworkerStartUpload.ReportProgress(100); asyncResult["IsCompleted"] = "true"; } else { } ftp.Disconnect(); ftp.Dispose(); // Change Local file name back to original string originalFilePath = fileInfo["LocalFilePath"].Replace(GlobalHelper.TempUploadFileExt, String.Empty); if (uploadSuccess || _cancelList.Contains(fileInfo["FileId"])) { File.Move(fileInfo["LocalFilePath"], originalFilePath); } if (uploadSuccess) { // Move local file to Recycle bin FileSystem.DeleteFile(originalFilePath, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin); // Remove temp extension from remote file string newName = remoteFilename.Replace(GlobalHelper.TempUploadFileExt, String.Empty); api.Rename(remoteFilename, newName); // Insert record into db int categoryId = Convert.ToInt32(fileInfo["ModelFileCategoryId"]); int lineId = Convert.ToInt32(fileInfo["ModelLineId"]); CFile.InsertOrUpdate(null, categoryId, lineId, fileInfo["ModelOriginFileName"], fileInfo["ModelFileName"], false, GlobalHelper.LoginUserID, fileInfo["ModelFileHash"]); } e.Result = asyncResult; }
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; }