您的位置:首页 > 其它

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

            }

        }

上传图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: