WCF 服务接受后编码的多部分/表单数据 [英] WCF service to accept a post encoded multipart/form-data

查看:31
本文介绍了WCF 服务接受后编码的多部分/表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道或最好有一个 WCF 服务的示例,该服务将接受表单后编码 multipart/form-data 即.从网页上传文件?

Does anyone know, or better yet have an example, of a WCF service that will accept a form post encoded multipart/form-data ie. a file upload from a web page?

我在谷歌上发现了空白.

I have come up empty on google.

他,蚂蚁

推荐答案

那么,这里...

创建您的服务合同,其中一个操作接受流作为其唯一参数,使用 WebInvoke 进行装饰,如下所示

Create your service contract which an operation which accepts a stream for its only parameter, decorate with WebInvoke as below

[ServiceContract]
public interface IService1 {

    [OperationContract]
    [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "/Upload")]
    void Upload(Stream data);

}

创建类...

    public class Service1 : IService1 {

    public void Upload(Stream data) {

        // Get header info from WebOperationContext.Current.IncomingRequest.Headers
        // open and decode the multipart data, save to the desired place
    }

和配置,接受流数据,以及最大大小

And the config, to accept streamed data, and the maximum size

<system.serviceModel>
   <bindings>
     <webHttpBinding>
       <binding name="WebConfiguration" 
                maxBufferSize="65536" 
                maxReceivedMessageSize="2000000000"
                transferMode="Streamed">
       </binding>
     </webHttpBinding>
   </bindings>
   <behaviors>
     <endpointBehaviors>
       <behavior name="WebBehavior">
         <webHttp />         
       </behavior>
     </endpointBehaviors>
     <serviceBehaviors>
       <behavior name="Sandbox.WCFUpload.Web.Service1Behavior">
         <serviceMetadata httpGetEnabled="true" httpGetUrl="" />
         <serviceDebug includeExceptionDetailInFaults="false" />
       </behavior>
     </serviceBehaviors>
   </behaviors>
   <services>     
     <service name="Sandbox.WCFUpload.Web.Service1" behaviorConfiguration="Sandbox.WCFUpload.Web.Service1Behavior">
      <endpoint 
        address=""
        binding="webHttpBinding" 
        behaviorConfiguration="WebBehavior"
        bindingConfiguration="WebConfiguration"
        contract="Sandbox.WCFUpload.Web.IService1" />
    </service>
  </services>
 </system.serviceModel>

也在 System.Web 中增加 System.Web 允许的数据量

Also in the System.Web increase the amount of data allowed in System.Web

<system.web>
        <otherStuff>...</otherStuff>
        <httpRuntime maxRequestLength="2000000"/>
</system.web>

这只是基础知识,但允许添加 Progress 方法来显示 ajax 进度条,您可能需要添加一些安全性.

This is just the basics, but allows for the addition of a Progress method to show an ajax progress bar and you may want to add some security.

这篇关于WCF 服务接受后编码的多部分/表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆