WCF REST 上传图片下载

服务器端

服务接口

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("下载完毕!");
            }
        }

上传图片


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值