前几天分享了分享了wcf聊天程序--wcfchat ,本文和大家一起分享利用wcf实现文件的传输。
程序运行效果:
接收文件端:
发送文件端:连接wcf服务,选择要传输的文件
文件传输成功:
我们会在保存文件的默认路径:c:\documents and settings\administrator\桌面,下看到传输的文件:
代码分析:
这里就不一一的阐述每一句代码的作用了,感兴趣的朋友可以下载,文后会有下载链接。说下值得注意的地方:
前两天有人在百度知道中问能不能把wcf中的契约单独封装到一个类库中,当时感觉多此一举,无意中看到把接口单独分出去,有个很好的应用,就是利用通道实现客户端代理。
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; //文件传输时间 } } |
利用通道创建客户端代理:
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); //利用通道创建客户端代理 _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 ; } |
这样,既不用添加服务引用,也不需要生成代理。
文件传输的函数不是很难,代码如下:
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( "数据流不可读!" ); } 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)); } |
其他的代码感兴趣的朋友下载来研究吧!