国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

分享WCF文件傳輸實現方法---WCFFileTransfer

2019-10-29 21:36:25
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了分享WCF文件傳輸實現方法---WCFFileTransfer,需要的朋友可以參考下

前幾天分享了分享了WCF聊天程序--WCFChat ,本文和大家一起分享利用WCF實現文件的傳輸。

程序運行效果:

接收文件端:

分享WCF文件傳輸實現方法---WCFFileTransfer

發送文件端:連接WCF服務,選擇要傳輸的文件

分享WCF文件傳輸實現方法---WCFFileTransfer

文件傳輸成功:

分享WCF文件傳輸實現方法---WCFFileTransfer

我們會在保存文件的默認路徑:C:/Documents and Settings/Administrator/桌面,下看到傳輸的文件:

分享WCF文件傳輸實現方法---WCFFileTransfer

代碼分析:

這里就不一一的闡述每一句代碼的作用了,感興趣的朋友可以下載,文后會有下載鏈接。說下值得注意的地方:

前兩天有人在百度知道中問能不能把WCF中的契約單獨封裝到一個類庫中,當時感覺多此一舉,無意中看到把接口單獨分出去,有個很好的應用,就是利用通道實現客戶端代理。

ITransfer.cs

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.ServiceModel; 
  6. using System.Runtime.Serialization; 
  7. using System.Threading; 
  8. using System.IO; 
  9.  
  10. namespace FileInterface 
  11. [ServiceContract] 
  12. public interface ITransfer 
  13. [OperationContract(Action = "UploadFile")] 
  14. void TransferFile(FileTransferMessage request);//文件傳輸 
  15.  
  16.  
  17. [MessageContract] 
  18. public class FileTransferMessage 
  19. [MessageHeader(MustUnderstand = true)] 
  20. public string SavePath;//文件保存路徑 
  21.  
  22. [MessageHeader(MustUnderstand = true)] 
  23. public string FileName;//文件名稱 
  24.  
  25. [MessageBodyMember(Order = 1)] 
  26. public Stream FileData;//文件傳輸時間 

利用通道創建客戶端代理:

 

 
  1. if (_proxy == null
  2. try 
  3. NetTcpBinding binding = new NetTcpBinding(); 
  4. binding.TransferMode = TransferMode.Streamed; 
  5. binding.SendTimeout = new TimeSpan(0, 30, 0); 
  6. //利用通道創建客戶端代理 
  7. _proxy = ChannelFactory<ITransfer>.CreateChannel(binding, new EndpointAddress(CBSerURL.Text)); 
  8. IContextChannel obj = _proxy as IContextChannel; 
  9. //string s = obj.SessionId; 
  10.  
  11. catch (Exception ex) 
  12. MessageBox.Show(ex.Message); 
  13. return

這樣,既不用添加服務引用,也不需要生成代理。

文件傳輸的函數不是很難,代碼如下:

 

 
  1. public void TransferFile(FileTransferMessage request) 
  2. string logInfo; 
  3.  
  4. Program.Get_ILog().Log(logInfo = string.Format("開始接收文件,name={0}", request.FileName));//填寫日志 
  5. //文件信息 
  6. string uploadFolder = AppValue.GetParam()._saveDir; 
  7. string savaPath = request.SavePath; 
  8. string fileName = request.FileName; 
  9. Stream sourceStream = request.FileData; 
  10. FileStream targetStream = null
  11. //判斷文件是否可讀 
  12. if (!sourceStream.CanRead) 
  13. throw new Exception("數據流不可讀!"); 
  14. if (savaPath == null) savaPath = @"文件傳輸/"
  15. if (!savaPath.EndsWith("//")) savaPath += "//"; 
  16. if (!uploadFolder.EndsWith("//")) uploadFolder += "//"; 
  17.  
  18. uploadFolder = uploadFolder + savaPath; 
  19. //創建保存文件夾 
  20. if (!Directory.Exists(uploadFolder)) 
  21. Directory.CreateDirectory(uploadFolder); 
  22.  
  23. int fileSize = 0; 
  24. string filePath = Path.Combine(uploadFolder, fileName);//Combine合并兩個路徑 
  25. try 
  26. //文件流傳輸 
  27. using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) 
  28. //定義文件緩沖區 
  29. const int bufferLen = 4096; 
  30. byte[] buffer = new byte[bufferLen]; 
  31. int count = 0; 
  32.  
  33. while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0) 
  34. targetStream.Write(buffer, 0, count); 
  35. fileSize += count; 
  36. targetStream.Close(); 
  37. sourceStream.Close(); 
  38. catch (Exception ex) 
  39. Program.Get_ILog().Log(logInfo + ex.Message); 
  40.  
  41. Program.Get_ILog().Log(string.Format("接收文件完畢 name={0},filesize={1}"
  42. request.FileName, fileSize)); 

其他的代碼感興趣的朋友下載來研究吧!


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松潘县| 高雄县| 慈利县| 易门县| 邳州市| 阳泉市| 海城市| 全州县| 凯里市| 唐海县| 富阳市| 蒙阴县| 井研县| 长顺县| 营山县| 封丘县| 竹溪县| 泉州市| 苗栗市| 浪卡子县| 屯昌县| 元谋县| 海淀区| 芦溪县| 贵阳市| 宁海县| 雅安市| 胶州市| 广饶县| 周口市| 元阳县| 贵南县| 察哈| 宝鸡市| 汶上县| 贵定县| 泽库县| 松潘县| 松潘县| 沙洋县| 双辽市|