介绍
WCF(Windows Communication Foundation) - 消息处理:MTOM(Message Transmission Optimization Mechanism) - 消息传输优化机制。本文以web方式上传大文件为例。
示例
1、服务
IMtom.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.IO; namespace WCF.ServiceLib.Message { /**//// /// IMtom接口 /// [ServiceContract] public interface IMtom { /**//// /// 上传文件 /// /// 文件目标路径 /// 文件字节数组 [OperationContract] void UploadFile(string path, byte[] fileData); } } |
Mtom.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.IO; namespace WCF.ServiceLib.Message { /**//// /// Mtom类 /// public class Mtom : IMtom { /**//// /// 上传文件 /// /// 文件目标路径 /// 文件字节数组 public void UploadFile(string path, byte[] fileData) { FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); fs.Write(fileData, 0, fileData.Length); fs.Flush(); fs.Close(); } } } |