private void UploadAttachment() { string fileServiceUrl = string.Empty; using (BugTraceEntities zentity = new BugTraceEntities(EntityContextHelper.GetEntityConnString())) { var currVersion = zentity.SYS_Version.Where(p => p.IsDefault == 1).FirstOrDefault(); fileServiceUrl = currVersion.FileServiceUrlPort; } try { string filepath = this.txtAttachment.Text; EndpointAddress address = new EndpointAddress("http://" + fileServiceUrl + "/JAJ.WinServer/FileService"); FileTransferSvc.FileServiceClient _client = new FileTransferSvc.FileServiceClient("BasicHttpBinding_IFileService", address); FileTransferSvc.TransferFileData uploadData = new FileTransferSvc.TransferFileData(); uploadData.FileName = Path.GetFileName(filepath); uploadData.FileData = File.OpenRead(filepath); uploadData.FileSize = (int)uploadData.FileData.Length; uploadData.FileUniqueID = this._attachmentUniqueID; uploadData.FileType = "PPM_ProblemTrace"; uploadData.oldFileName = this._oldFileName; wait = new FrmWait("正 在 上 传 请 稍 候"); wait.Show(); _client.UploadFileAsync(uploadData.FileName, uploadData.FileSize, uploadData.FileType, uploadData.FileUniqueID, uploadData.oldFileName, uploadData.FileData); _client.UploadFileCompleted += _client_UploadFileCompleted; } catch (Exception ex) { MessageBox.Show("文件上传失败!"); MyLog.LogError("保存问题跟踪时文件上传失败!", ex); return; } }
private void downloadFile(object rowIndex) { int BufferLen = 4096; string folder = folderBrowserDialog1.SelectedPath; string file = this.dgvShareFile.Rows[(int)rowIndex].Cells["FilePath"].Value.ToString(); string fileName = Path.GetFileName(file); string fileServiceUrl = string.Empty; using (BugTraceEntities zentity = new BugTraceEntities(EntityContextHelper.GetEntityConnString())) { var currVersion = zentity.SYS_Version.Where(p => p.IsDefault == 1).FirstOrDefault(); fileServiceUrl = currVersion.FileServiceUrlPort; } Stream sourceStream = null; try { EndpointAddress address = new EndpointAddress("http://" + fileServiceUrl + "/JAJ.WinServer/FileService"); FileTransferSvc.FileServiceClient _client = new FileTransferSvc.FileServiceClient("BasicHttpBinding_IFileService", address); sourceStream = _client.DowndloadFile(file); } catch (Exception ex) { MyLog.LogError("文件下载失败!", ex); return; } FileStream targetStream = null; if (!sourceStream.CanRead) { MyLog.LogError("下载异常", new Exception("Invalid Stream!")); throw new Exception("Invalid Stream!"); } string filePath = Path.Combine(folder, fileName); using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { byte[] buffer = new byte[BufferLen]; int count = 0; while ((count = sourceStream.Read(buffer, 0, BufferLen)) > 0) { targetStream.Write(buffer, 0, count); } targetStream.Close(); sourceStream.Close(); } this.Invoke(new MethodInvoker(() => { try { wait.Close(); } catch { } MessageBox.Show("下载成功!"); })); }
private void FrmUploadFile_Load(object sender, EventArgs e) { _client = new FileTransferSvc.FileServiceClient(); _uploadCompleted = false; _updateLabel = (curr, total) => { lblInfo.Text = string.Format("{0}KB/{1}KB", curr / 1000, total / 1000); }; _updateProcessBar = (process) => { progressBar1.Value = process; }; }
private void FrmDownloadFile_Load(object sender, EventArgs e) { _client = new FileTransferSvc.FileServiceClient(); }
private void downloadFile(object obj) { List <KeyValuePair <string, string> > map = obj as List <KeyValuePair <string, string> >; int BufferLen = 4096; string folder = map.Where(p => p.Key == "folder").FirstOrDefault().Value; string fileName = map.Where(p => p.Key == "fileName").FirstOrDefault().Value; string uniqueID = map.Where(p => p.Key == "uniqueID").FirstOrDefault().Value; string serverFilePath = "\\UploadFile\\PPM_ProblemTrace\\" + uniqueID + fileName; string fileServiceUrl = string.Empty; using (BugTraceEntities zentity = new BugTraceEntities(EntityContextHelper.GetEntityConnString())) { var currVersion = zentity.SYS_Version.Where(p => p.IsDefault == 1).FirstOrDefault(); fileServiceUrl = currVersion.FileServiceUrlPort; } Stream sourceStream = null; try { EndpointAddress address = new EndpointAddress("http://" + fileServiceUrl + "/JAJ.WinServer/FileService"); FileTransferSvc.FileServiceClient _client = new FileTransferSvc.FileServiceClient("BasicHttpBinding_IFileService", address); sourceStream = _client.DowndloadFile(serverFilePath); } catch (Exception ex) { MyLog.LogError("文件下载失败!", ex); this.Invoke(new MethodInvoker(() => { try { wait.Close(); } catch { } MessageBox.Show("文件下载失败!"); })); return; } FileStream targetStream = null; if (!sourceStream.CanRead) { MyLog.LogError("下载异常", new Exception("Invalid Stream!")); throw new Exception("Invalid Stream!"); } string filePath = Path.Combine(folder, fileName); using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { byte[] buffer = new byte[BufferLen]; int count = 0; while ((count = sourceStream.Read(buffer, 0, BufferLen)) > 0) { targetStream.Write(buffer, 0, count); } targetStream.Close(); sourceStream.Close(); } this.Invoke(new MethodInvoker(() => { try { wait.Close(); } catch { } MessageBox.Show("下载成功!"); })); }