虽然有许多人对WCF文件的使用性表示怀疑,但在年复一年的不断发展中,但前提是要深入理解到底什么是WCF文件,及WCF是怎么运作的。 下面就对WCF文件进行深一步的介绍。。。
这样,既不用添加服务引用,也不需要生成代理。文件传输的函数不是很难,代码如下:
- publicvoidTransferFile(FileTransferMessagerequest)
- {
- stringlogInfo;
- Program.Get_ILog().Log(logInfo=string.Format("开始接收文件,name={0}",request.FileName));//填写日志
- //文件信息
- stringuploadFolder=AppValue.GetParam()._saveDir;
- stringsavaPath=request.SavePath;
- stringfileName=request.FileName;
- StreamsourceStream=request.FileData;
- FileStreamtargetStream=null;
- //判断文件是否可读
- if(!sourceStream.CanRead)
- {
- thrownewException("数据流不可读!");
- }
- if(savaPath==null)savaPath=@"文件传输\";
- if(!savaPath.EndsWith("\\"))savaPath+="\\";
- if(!uploadFolder.EndsWith("\\"))uploadFolder+="\\";
- uploadFolderuploadFolder=uploadFolder+savaPath;
- //创建保存文件夹
- if(!Directory.Exists(uploadFolder))
- {
- Directory.CreateDirectory(uploadFolder);
- }
- intfileSize=0;
- stringfilePath=Path.Combine(uploadFolder,fileName);//Combine合并两个路径
- try
- {
- 文件流传输
- using(targetStream=newFileStream(filePath,FileMode.Create,FileAccess.Write,FileShare.None))
- {
- 定义文件缓冲区
- constintbufferLen=4096;
- byte[]buffer=newbyte[bufferLen];
- intcount=0;
- while((count=sourceStream.Read(buffer,0,bufferLen))>0)
- {
- targetStream.Write(buffer,0,count);
- fileSize+=count;
- }
- targetStream.Close();
- sourceStream.Close();
- }
- }
- catch(Exceptionex)
- {
- Program.Get_ILog().Log(logInfo+ex.Message);
- }
- Program.Get_ILog().Log(string.Format("接收文件完毕name={0},filesize={1}",
- request.FileName,fileSize));
- }
WCF文件其他的代码感兴趣的朋友下载来研究吧!
【编辑推荐】