html - WCF 服务接受后编码的多部分/表单数据

标签 html wcf http

有没有人知道,或者更好的是有一个 WCF 服务的例子,它将接受一个表单后编码 multipart/form-data 即。从网页上传文件?

我在谷歌上空空如也。

Ant , Ant

最佳答案

所以,这里...

创建您的服务合约,其中一个操作接受流作为其唯一参数,用 WebInvoke 装饰如下

[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
    }

和配置,接受流式数据,和最大尺寸

<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 中允许的数据量

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

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

关于html - WCF 服务接受后编码的多部分/表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1354749/

相关文章:

c# - 将字符串查询传递给wcf并从db检索数据

iphone - 服务器不接受我的多部分/表单数据

java - 可用的不同 HttpClient 之间有什么区别?

javascript - 在 HTML 中放置条件

html - 重复的标题标签?

c# - WCF 休息 : What is it expecting my XML to look like in requests?

Perl WWW::Mechanize 将 cookie 存储为字符串

javascript - 单击时输出字段和按钮消失

html - 在 Wordpress 发布图像中覆盖自动样式

c# - 重用 try catch 进行 wcf 调用