2009-08-30 71 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