WCF REST 上传图片下载
2015-09-17 17:06
375 查看
服务器端
服务接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.IO;
using System.ServiceModel.Web;
namespace EFTest.Test.WCFRest.IServicel
{
[ServiceContract]
public interface IUploadImg
{
//其中ReadImg方法用于提供jpg图片文件,供客户端下载,而ReceiveImg用于接收客户端上传的jpg图片
[OperationContract]
[WebInvoke(Method="*",UriTemplate="ReadImg")]
System.IO.Stream ReadImg();
[OperationContract]
[WebInvoke(Method="*",UriTemplate="ReceiveImg")]
void ReceiveImg(System.IO.Stream stream);
}
}
实现接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Web;
using System.Drawing;
using System.Diagnostics;
namespace EFTest.Test.WCFRest.Service
{
public class UploadImgService : EFTest.Test.WCFRest.IServicel.IUploadImg
{
public System.IO.Stream ReadImg()
{
string runDir = System.Environment.CurrentDirectory;
string Imgfilepath=System.IO.Path.Combine(@"E:\c#练习\ZXEFTest","img.jpg");
System.IO.FileStream fs = new System.IO.FileStream(Imgfilepath, System.IO.FileMode.Open);
System.Threading.Thread.Sleep(2000);
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpg";
return fs;
}
public void ReceiveImg(System.IO.Stream stream)
{
// Debug.WriteLine(WebOperationContext.Current.IncomingRequest.ContentType);
System.Threading.Thread.Sleep(3000);
string runDir = System.Environment.CurrentDirectory;
string imgFilePath = System.IO.Path.Combine(runDir, "ReceiveImg.jpg");
Image bmp = Bitmap.FromStream(stream);
bmp.Save(imgFilePath);
}
}
}
web.config或者app.config,如果上传超过68K图片就要设置maxReceivedMessageSize 值,这是最重要的
<bindings>
<webHttpBinding>
<binding name="ServiceBinding" receiveTimeout="00:10:00" sendTimeout ="00:10:00" maxReceivedMessageSize="2147483647" >
<readerQuotas maxStringContentLength="2147483647" />
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="EFTest.Test.WCFRest.Service.UploadImgService">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:5050/"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" bindingConfiguration="ServiceBinding" contract="EFTest.Test.WCFRest.IServicel.IUploadImg" behaviorConfiguration="WcfBinaryRestfulBehavior" >
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WcfBinaryRestfulBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
客户端
不用设置app.config或者web.config
主要用 System.Net.WebClient对象连接接收
接收图片
public Form1()
{
InitializeComponent();
}
System.Net.WebClient wc;
private void button1_Click(object sender, EventArgs e)
{
wc = new System.Net.WebClient();
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
wc.DownloadFileAsync(new Uri("http://127.0.0.1:5050/"), sfd.FileName);
wc.DownloadFileCompleted+=new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
}
}
void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
// button2.Enabled = false;
button1.Enabled = true;
MessageBox.Show("用户已经取消下载!");
return;
}
using (System.Net.WebClient wc = e.UserState as System.Net.WebClient)
{
// button2.Enabled = false;
button1.Enabled = false;
MessageBox.Show("下载完毕!");
}
}
上传图片
服务接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.IO;
using System.ServiceModel.Web;
namespace EFTest.Test.WCFRest.IServicel
{
[ServiceContract]
public interface IUploadImg
{
//其中ReadImg方法用于提供jpg图片文件,供客户端下载,而ReceiveImg用于接收客户端上传的jpg图片
[OperationContract]
[WebInvoke(Method="*",UriTemplate="ReadImg")]
System.IO.Stream ReadImg();
[OperationContract]
[WebInvoke(Method="*",UriTemplate="ReceiveImg")]
void ReceiveImg(System.IO.Stream stream);
}
}
实现接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Web;
using System.Drawing;
using System.Diagnostics;
namespace EFTest.Test.WCFRest.Service
{
public class UploadImgService : EFTest.Test.WCFRest.IServicel.IUploadImg
{
public System.IO.Stream ReadImg()
{
string runDir = System.Environment.CurrentDirectory;
string Imgfilepath=System.IO.Path.Combine(@"E:\c#练习\ZXEFTest","img.jpg");
System.IO.FileStream fs = new System.IO.FileStream(Imgfilepath, System.IO.FileMode.Open);
System.Threading.Thread.Sleep(2000);
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpg";
return fs;
}
public void ReceiveImg(System.IO.Stream stream)
{
// Debug.WriteLine(WebOperationContext.Current.IncomingRequest.ContentType);
System.Threading.Thread.Sleep(3000);
string runDir = System.Environment.CurrentDirectory;
string imgFilePath = System.IO.Path.Combine(runDir, "ReceiveImg.jpg");
Image bmp = Bitmap.FromStream(stream);
bmp.Save(imgFilePath);
}
}
}
web.config或者app.config,如果上传超过68K图片就要设置maxReceivedMessageSize 值,这是最重要的
<bindings>
<webHttpBinding>
<binding name="ServiceBinding" receiveTimeout="00:10:00" sendTimeout ="00:10:00" maxReceivedMessageSize="2147483647" >
<readerQuotas maxStringContentLength="2147483647" />
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="EFTest.Test.WCFRest.Service.UploadImgService">
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:5050/"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" bindingConfiguration="ServiceBinding" contract="EFTest.Test.WCFRest.IServicel.IUploadImg" behaviorConfiguration="WcfBinaryRestfulBehavior" >
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WcfBinaryRestfulBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
客户端
不用设置app.config或者web.config
主要用 System.Net.WebClient对象连接接收
接收图片
public Form1()
{
InitializeComponent();
}
System.Net.WebClient wc;
private void button1_Click(object sender, EventArgs e)
{
wc = new System.Net.WebClient();
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
wc.DownloadFileAsync(new Uri("http://127.0.0.1:5050/"), sfd.FileName);
wc.DownloadFileCompleted+=new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
}
}
void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
// button2.Enabled = false;
button1.Enabled = true;
MessageBox.Show("用户已经取消下载!");
return;
}
using (System.Net.WebClient wc = e.UserState as System.Net.WebClient)
{
// button2.Enabled = false;
button1.Enabled = false;
MessageBox.Show("下载完毕!");
}
}
上传图片
相关文章推荐
- APK动态分析解析
- lucene搜索引擎总结
- Vim命令合集
- 解决Android手机自带内部存储路径存在但是却获取不到内容的问题
- 多线程之生产者消费者问题
- Request中的getParameter和getAttribute的区别是什么?
- javaScript中的页面传值
- android presentation
- 学习日志---查找算法
- SQL基础教程之SQL指令
- 支付宝第三方支付 平台
- MVC浅析(实际上应该是MVP,有时间再更新该博客)
- 文本渐变
- return false 无效
- Android httpClient 支持HTTPS
- redis缓存的安装和使用
- Linux下查看文件和文件夹大小
- c# 字符串切割 split
- 使用RelativeLayout控制WebView以及Bottom按钮的位置
- PCA主成分分析