分享WCF文件傳輸實(shí)現(xiàn)方法---WCFFileTransfer
前幾天分享了分享了WCF聊天程序--WCFChat ,本文和大家一起分享利用WCF實(shí)現(xiàn)文件的傳輸。
程序運(yùn)行效果:
接收文件端:
發(fā)送文件端:連接WCF服務(wù),選擇要傳輸?shù)奈募?br />
文件傳輸成功:
我們會(huì)在保存文件的默認(rèn)路徑:C:\Documents and Settings\Administrator\桌面,下看到傳輸?shù)奈募?
代碼分析:
這里就不一一的闡述每一句代碼的作用了,感興趣的朋友可以下載,文后會(huì)有下載鏈接。說(shuō)下值得注意的地方:
前兩天有人在百度知道中問(wèn)能不能把WCF中的契約單獨(dú)封裝到一個(gè)類庫(kù)中,當(dāng)時(shí)感覺(jué)多此一舉,無(wú)意中看到把接口單獨(dú)分出去,有個(gè)很好的應(yīng)用,就是利用通道實(shí)現(xiàn)客戶端代理。
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;//文件傳輸時(shí)間 } }
利用通道創(chuàng)建客戶端代理:
if (_proxy == null) { try { NetTcpBinding binding = new NetTcpBinding(); binding.TransferMode = TransferMode.Streamed; binding.SendTimeout = new TimeSpan(0, 30, 0); //利用通道創(chuàng)建客戶端代理 _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; }
這樣,既不用添加服務(wù)引用,也不需要生成代理。
文件傳輸?shù)暮瘮?shù)不是很難,代碼如下:
public void TransferFile(FileTransferMessage request) { string logInfo; Program.Get_ILog().Log(logInfo = string.Format("開(kāi)始接收文件,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("數(shù)據(jù)流不可讀!"); } if (savaPath == null) savaPath = @"文件傳輸\"; if (!savaPath.EndsWith("\\")) savaPath += "\\"; if (!uploadFolder.EndsWith("\\")) uploadFolder += "\\"; uploadFolder = uploadFolder + savaPath; //創(chuàng)建保存文件夾 if (!Directory.Exists(uploadFolder)) { Directory.CreateDirectory(uploadFolder); } int fileSize = 0; string filePath = Path.Combine(uploadFolder, fileName);//Combine合并兩個(gè)路徑 try { //文件流傳輸 using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { //定義文件緩沖區(qū) 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)); }
其他的代碼感興趣的朋友下載來(lái)研究吧!
源代碼:WCFFileTransfer.rar
欄 目:C#教程
下一篇:C#編程實(shí)現(xiàn)對(duì)象與JSON串互相轉(zhuǎn)換實(shí)例分析
本文標(biāo)題:分享WCF文件傳輸實(shí)現(xiàn)方法---WCFFileTransfer
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6856.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#實(shí)現(xiàn)讀取被進(jìn)程占用的文件實(shí)現(xiàn)方法
- 01-10C#刪除只讀文件或文件夾(解決File.Delete無(wú)法刪除文件)
- 01-10C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
- 01-10C#路徑,文件,目錄及IO常見(jiàn)操作匯總
- 01-10winform實(shí)現(xiàn)拖動(dòng)文件到窗體上的方法
- 01-10WPF實(shí)現(xiàn)類似360安全衛(wèi)士界面的程序源碼分享


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子