2009-08-30 56 views
26

有谁知道,或者更好的例子,WCF服务将接受编码为multipart/form-data的表格文章。从网页上传文件?WCF服务接受编码后的多部分/表格数据

我在谷歌上空了。

钽,蚂蚁

+0

看到我的答案在这里:http://stackoverflow.com/a/21689347/67824 – 2014-02-10 22:39:27

+0

这个链接对我来说,我希望你会从中得到一些想法。 http://stackoverflow.com/questions/7460088/reading-file-input-from-a-multipart-form-data-post/14514351#14514351 – 2014-02-17 14:39:16

回答

57

所以,在这里去...

创建您的服务合同,并同意对唯一参数流的操作,以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.Web最大尺寸

<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> 
     <otherStuff>...</otherStuff> 
     <httpRuntime maxRequestLength="2000000"/> 
</system.web> 

这仅仅是基础,但允许添加进展方法来显示ajax进度条,并且您可能想要添加一些安全性。

+2

如何删除所有正在使用流发送的垃圾,如: 内容处置:,内容类型:等...我试图让这个工作的图像。另外为什么不能在合同定义 – Adam 2011-05-29 17:37:01

+0

任何其他参数任何想法如何使用肥皂这项工作? – Gluip 2013-01-16 14:03:05

1

我并不确切地知道你要在这里完成的,但没有内置的“经典”基于SOAP的WCF支持捕获和处理表单提交的数据。你必须自己做。另一方面,如果你正在谈论基于REST的WCF和webHttpBinding,你当然可以有一个服务方法,用[WebInvoke()]属性来装饰,这个方法将用一个HTTP POST方法调用。

[WebInvoke(Method="POST", UriTemplate="....")] 
    public string PostHandler(int value) 

URI模板将定义要在HTTP POST应该去的地方使用的URI。你必须将它与你的ASP.NET表单(或者你正在使用的任何实际发布的内容)联系起来。

有关REST风格WCF的详细介绍,请查看WCF REST入门工具包上的Aaron Skonnard的screen cast series以及如何使用它。

马克

+1

嗨马克, 我想有一个宁静的wcf服务可以接受来自HTML表单的发布数据,该表单中包含[input type = file /]。 我已经能够发布没有文件的表单。 我不希望客户端应用只是浏览器,所以我不能将文件转换为字节流,它将是一个multipart/form-data http post – 2009-08-31 10:41:42