基本信息
源码名称:C# WCF REST方式 上传下载文件
源码大小:0.03M
文件格式:.zip
开发语言:C#
更新时间:2016-02-04
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
如您在本机测试的时候 切记
1.运行的时候 以 管理员身份运行
2.将Client项目中的 ip地址 192.168.1.8 改成你本机ip或者localhost
using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Contracts; using System.Drawing; using System.Linq; using System.Reflection; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Web; using System.Text; using WcfBinaryRestful.Contracts; namespace WcfBinaryRestful.Service { public class Service : IService { public string GetData(int value) { throw new NotImplementedException(); } [WebInvoke(Method = "*", UriTemplate = "readimg")] public System.IO.Stream ReadImg() { string runDir = "d:\\"; string imgFilePath = System.IO.Path.Combine(runDir, "down.jpg"); System.IO.FileStream fs = new System.IO.FileStream(imgFilePath, System.IO.FileMode.Open); System.Threading.Thread.Sleep(2000); WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; return fs; } [WebInvoke(Method = "*", UriTemplate = "receiveimg")] public void ReceiveImg(System.IO.Stream stream) { Debug.WriteLine(WebOperationContext.Current.IncomingRequest.ContentType); System.Threading.Thread.Sleep(2000); string runDir = "d:\\"; string imgFilePath = System.IO.Path.Combine(runDir, "up.jpg"); Image bmp = Bitmap.FromStream(stream); bmp.Save(imgFilePath); } [WebInvoke(Method = "*", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, UriTemplate = "/receiveimage/{id}")] public void ReceiveImage(string id, System.IO.Stream stream) { Debug.WriteLine(WebOperationContext.Current.IncomingRequest.ContentType); System.Threading.Thread.Sleep(2000); string runDir = "d:\\"; string imgFilePath = System.IO.Path.Combine(runDir, id ".jpg"); Image bmp = Bitmap.FromStream(stream); bmp.Save(imgFilePath); } [WebInvoke(Method = "*", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, UriTemplate = "/readimage/{filename}")] public System.IO.Stream ReadImage(string filename) { string runDir = "d:\\"; string imgFilePath = System.IO.Path.Combine(runDir, filename); System.IO.FileStream fs = new System.IO.FileStream(imgFilePath, System.IO.FileMode.Open); WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; return fs; } [WebInvoke(Method = "*", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, UriTemplate = "/hello/{hi}")] public string Hello(string hi) { string methodname = GetMethodName(); OperationContext context = OperationContext.Current; MessageProperties properties = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; Console.WriteLine(string.Format("{3} - Hello {0},You are from {1}:{2}", hi, endpoint.Address, endpoint.Port, methodname)); return string.Format("{3} - Hello {0},You are from {1}:{2}", hi, endpoint.Address, endpoint.Port, methodname); } public static string GetMethodName() { var method = new StackFrame(1).GetMethod(); // 这里忽略1层堆栈,也就忽略了当前方法GetMethodName,这样拿到的就正好是外部调用GetMethodName的方法信息 var property = ( from p in method.DeclaringType.GetProperties( BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) where p.GetGetMethod(true) == method || p.GetSetMethod(true) == method select p).FirstOrDefault(); return property == null ? method.Name : property.Name; } } }