// Advance:執行把資料建置到Listview private void GenerateListviewItem(int page) { lvwAdvanceClassify.ItemsSource = null; lvwAdvanceClassify.Items.Clear(); cmbPager.ItemsSource = null; _dataPager.Clear(); dynamic files = GetAdvanceFileList(); int totalRecordNum = Enumerable.Count(files); _advCurrentPage = page; if (totalRecordNum <= _advPageSize) { _advTotalPage = 1; cmbPager.Visibility = System.Windows.Visibility.Hidden; } else { _advTotalPage = totalRecordNum % _advPageSize == 0 ? totalRecordNum / _advPageSize : totalRecordNum / _advPageSize + 1; if (_advTotalPage == 0) _advTotalPage = 1; for (int i = 1; i <= _advTotalPage; i++) { _dataPager.Add(i); } cmbPager.Visibility = System.Windows.Visibility.Visible; cmbPager.SelectedItem = page ; } cmbPager.ItemsSource = _dataPager; int skipNum = (_advCurrentPage - 1) * _advPageSize; files = GetAdvanceFileList(skipNum, _advPageSize); // For list mode datasource System.Collections.ObjectModel.ObservableCollection<FileInfo> fileCollection = new System.Collections.ObjectModel.ObservableCollection<FileInfo>(); ApiHelper api = new ApiHelper(); foreach (var file in files) { if (_isAdvanceTileView) { bool isImageFile = false; lvwAdvanceClassify.View = lvwAdvanceClassify.FindResource("TileView") as ViewBase; // Using store procedure to get full path. string path = DBHelper.GenerateFileFullPath(file.Id); if (api.CheckPath(path)) { BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.CacheOption = BitmapCacheOption.OnLoad; ExtensionHelper helper = new ExtensionHelper(); string iconPath = helper.GetIconPath( System.IO.Path.GetExtension(file.NickName)); if (iconPath != "img.ico") { bitmap.UriSource = new Uri(iconPath); } else { isImageFile = true; bitmap.UriSource = new Uri(String.Format(GlobalHelper.ApiThumb, path)); } bitmap.EndInit(); Image img = new Image(); if (!isImageFile) { img.Width = 60; img.Height = 60; } else { img.Width = 120; img.Height = 120; } img.Source = bitmap; string title = Convert.ToString(file.NickName); Tile tile = new Tile(); tile.FontFamily = new FontFamily("Microsoft JhengHei"); tile.Width = 120; tile.Height = 120; tile.Margin = new Thickness(5); tile.Content = img; tile.Tag = path; // Download Path tile.ToolTip = file.NickName; tile.Click += new RoutedEventHandler(tileAdvance_Click); tile.MouseDoubleClick += new MouseButtonEventHandler(tileAdvance_MouseDoubleClick); if (isImageFile) { tile.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)); } else { tile.Title = title.Length > 12 ? String.Format("{0}…", title.Substring(0, 11)) : title; } lvwAdvanceClassify.Items.Add(tile); } } else { lvwAdvanceClassify.View = lvwAdvanceClassify.FindResource("AdvanceListView") as ViewBase; string path = DBHelper.GenerateFileFullPath(file.Id); if (api.CheckPath(path)) { fileCollection.Add(new FileInfo { FileName = file.Name, FilePath = DBHelper.GenerateFileFullPath(file.Id), FileId = file.Id }); } } } // 修改Listview Datasource if (!_isAdvanceTileView) { lvwAdvanceClassify.ItemsSource = fileCollection; } if (lvwAdvanceClassify.Items.Count == 0) { lbMessage.Visibility = System.Windows.Visibility.Visible; lbMessage.Content = "搜尋完成,無資料紀錄"; } else { lbMessage.Visibility = System.Windows.Visibility.Hidden; lbMessage.Content = ""; } }
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 ReadProgressList() { // Read progress list List<ProgressInfo> progressList = JsonConvert.DeserializeObject<List<ProgressInfo>>( File.ReadAllText(GlobalHelper.ProgressList)).Select(c => (ProgressInfo)c).ToList(); List<ProgressInfo> tmpProgressList = new List<ProgressInfo>(); tmpProgressList.AddRange(progressList); if (progressList.Count > 0) { ApiHelper api = new ApiHelper(); foreach (ProgressInfo info in progressList) { // 如果本地端未完成檔案存在才顯示於進度清單中,否則就將該筆進度刪除 if (File.Exists(info.LocalFilePath) && api.CheckPath(info.RemoteFilePath)) { if (info.Type.Equals("Download")) { long localFileSize = new FileInfo(info.LocalFilePath).Length; int percentage = Convert.ToInt32(((double)localFileSize / (double)info.FileSize) * 100); _dataDownloadFiles.Add(new FileProgressItem { Name = System.IO.Path.GetFileName(info.LocalFilePath).Replace(GlobalHelper.TempDownloadFileExt, String.Empty), Progress = percentage, FileId = info.FileId }); } else { long remoteFileSize = api.GetFileSize(info.RemoteFilePath); int percentage = Convert.ToInt32(((double)remoteFileSize / (double)info.FileSize) * 100); _dataUploadFiles.Add(new FileProgressItem { Name = System.IO.Path.GetFileName(info.LocalFilePath).Replace(GlobalHelper.TempUploadFileExt, String.Empty), Progress = percentage, FileId = info.FileId }); } } else { int indexOfFile = tmpProgressList.IndexOf(info); tmpProgressList.RemoveAt(indexOfFile); } } // Serialize progress list to json format string jsonList = JsonConvert.SerializeObject(tmpProgressList, Formatting.Indented); // Overwrite progress list File.WriteAllText(GlobalHelper.ProgressList, jsonList, Encoding.UTF8); } }
/// <summary> /// 處理取得路徑 /// </summary> private void GetPath() { if (String.IsNullOrEmpty(txtName.Text)|| String.IsNullOrEmpty(txtNickName.Text)) { lbMessage.Content = "欄位不得為空白"; return; } if (!Regex.IsMatch(txtName.Text.Trim(), PATTERN_SYSTEMNAME)) { lbMessage.Content = "系統名稱格式錯誤"; return; } else { lbMessage.Content = ""; } if (!Regex.IsMatch(txtNickName.Text.Trim(), PATTERN_NICKNAME)) { lbMessage.Content = "瀏覽名稱格式錯誤"; return; } else { lbMessage.Content = ""; } this.SystemName = txtName.Text.Trim(); this.NickName = txtNickName.Text.Trim(); string path = this.PrePath + "/" + txtName.Text.Trim(); // 判斷如果有這個名稱則提醒 ApiHelper api = new ApiHelper(); if (!api.CheckPath(path)) { txtName.Foreground = Brushes.Black; lbMessage.Content = ""; this.IsDone = true; this.Close(); } else { txtName.Foreground = Brushes.Red; lbMessage.Content = "此系統名稱已存在,請換別的名稱"; } }