這篇文章主要介紹了分享WCF文件傳輸實現方法---WCFFileTransfer,需要的朋友可以參考下
前幾天分享了分享了WCF聊天程序--WCFChat ,本文和大家一起分享利用WCF實現文件的傳輸。
程序運行效果:
接收文件端:
發送文件端:連接WCF服務,選擇要傳輸的文件
文件傳輸成功:
我們會在保存文件的默認路徑:C:/Documents and Settings/Administrator/桌面,下看到傳輸的文件:
代碼分析:
這里就不一一的闡述每一句代碼的作用了,感興趣的朋友可以下載,文后會有下載鏈接。說下值得注意的地方:
前兩天有人在百度知道中問能不能把WCF中的契約單獨封裝到一個類庫中,當時感覺多此一舉,無意中看到把接口單獨分出去,有個很好的應用,就是利用通道實現客戶端代理。
ITransfer.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using System.Runtime.Serialization;
- using System.Threading;
- using System.IO;
- namespace FileInterface
- {
- [ServiceContract]
- public interface ITransfer
- {
- [OperationContract(Action = "UploadFile")]
- void TransferFile(FileTransferMessage request);//文件傳輸
- }
- [MessageContract]
- public class FileTransferMessage
- {
- [MessageHeader(MustUnderstand = true)]
- public string SavePath;//文件保存路徑
- [MessageHeader(MustUnderstand = true)]
- public string FileName;//文件名稱
- [MessageBodyMember(Order = 1)]
- public Stream FileData;//文件傳輸時間
- }
- }
利用通道創建客戶端代理:
- if (_proxy == null)
- {
- try
- {
- NetTcpBinding binding = new NetTcpBinding();
- binding.TransferMode = TransferMode.Streamed;
- binding.SendTimeout = new TimeSpan(0, 30, 0);
- //利用通道創建客戶端代理
- _proxy = ChannelFactory<ITransfer>.CreateChannel(binding, new EndpointAddress(CBSerURL.Text));
- IContextChannel obj = _proxy as IContextChannel;
- //string s = obj.SessionId;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- return;
- }
這樣,既不用添加服務引用,也不需要生成代理。
文件傳輸的函數不是很難,代碼如下:
- public void TransferFile(FileTransferMessage request)
- {
- string logInfo;
- Program.Get_ILog().Log(logInfo = string.Format("開始接收文件,name={0}", request.FileName));//填寫日志
- //文件信息
- string uploadFolder = AppValue.GetParam()._saveDir;
- string savaPath = request.SavePath;
- string fileName = request.FileName;
- Stream sourceStream = request.FileData;
- FileStream targetStream = null;
- //判斷文件是否可讀
- if (!sourceStream.CanRead)
- {
- throw new Exception("數據流不可讀!");
- }
- if (savaPath == null) savaPath = @"文件傳輸/";
- if (!savaPath.EndsWith("//")) savaPath += "//";
- if (!uploadFolder.EndsWith("//")) uploadFolder += "//";
- uploadFolder = uploadFolder + savaPath;
- //創建保存文件夾
- if (!Directory.Exists(uploadFolder))
- {
- Directory.CreateDirectory(uploadFolder);
- }
- int fileSize = 0;
- string filePath = Path.Combine(uploadFolder, fileName);//Combine合并兩個路徑
- try
- {
- //文件流傳輸
- using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
- {
- //定義文件緩沖區
- const int bufferLen = 4096;
- byte[] buffer = new byte[bufferLen];
- int count = 0;
- while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
- {
- targetStream.Write(buffer, 0, count);
- fileSize += count;
- }
- targetStream.Close();
- sourceStream.Close();
- }
- }
- catch (Exception ex)
- {
- Program.Get_ILog().Log(logInfo + ex.Message);
- }
- Program.Get_ILog().Log(string.Format("接收文件完畢 name={0},filesize={1}",
- request.FileName, fileSize));
- }
其他的代碼感興趣的朋友下載來研究吧!
新聞熱點
疑難解答