前幾天分享了分享了wcf聊天程序--wcfchat ,本文和大家一起分享利用wcf實(shí)現(xiàn)文件的傳輸。
程序運(yùn)行效果:
接收文件端:
發(fā)送文件端:連接wcf服務(wù),選擇要傳輸?shù)奈募?br />
文件傳輸成功:
我們會(huì)在保存文件的默認(rèn)路徑:c:\documents and settings\administrator\桌面,下看到傳輸?shù)奈募?
代碼分析:
這里就不一一的闡述每一句代碼的作用了,感興趣的朋友可以下載,文后會(huì)有下載鏈接。說下值得注意的地方:
前兩天有人在百度知道中問能不能把wcf中的契約單獨(dú)封裝到一個(gè)類庫(kù)中,當(dāng)時(shí)感覺多此一舉,無意中看到把接口單獨(dú)分出去,有個(gè)很好的應(yīng)用,就是利用通道實(shí)現(xiàn)客戶端代理。
itransfer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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)建客戶端代理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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ù)不是很難,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
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( "數(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)); } |
其他的代碼感興趣的朋友下載來研究吧!