WCF服务接受一个后编码的多部分/表单数据 [英] WCF service to accept a post encoded multipart/form-data
问题描述
有谁知道,或者更好的例子是一个WCF服务,它将接受编码为 multipart / form-data
的表单过帐。从网页文件上传?
我在google上空了。
Ta,Ant
所以,这里就是...
创建您的服务合约这是一个接受流的唯一参数的操作,如下所示用WebInvoke装饰
[ServiceContract]
public interface IService1 {
$ b $ [OperationContract]
[WebInvoke(
Method =POST,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate =/ Upload )]
无效上传(流数据);
创建类...
public class Service1:IService1 {
public void Upload(流数据){
//从WebOperationContext.Current.IncomingRequest.Headers获取标题信息
//打开并解码多部分数据,保存到所需位置
}
以及配置,接受流数据以及最大大小
< system.serviceModel>
< bindings>
< webHttpBinding>
< binding name =WebConfiguration
maxBufferSize =65536
maxReceivedMessageSize =2000000000
transferMode =Streamed>
< / binding>
< / webHttpBinding>
< / bindings>
<行为>
< endpointBehaviors>
< behavior name =WebBehavior>
< webHttp />
< /行为>
< / endpointBehaviors>
< serviceBehaviors>
< behavior name =Sandbox.WCFUpload.Web.Service1Behavior>
< serviceMetadata httpGetEnabled =truehttpGetUrl =/>
< serviceDebug includeExceptionDetailInFaults =false/>
< /行为>
< / serviceBehaviors>
< /行为>
< services>
< service name =Sandbox.WCFUpload.Web.Service1behaviorConfiguration =Sandbox.WCFUpload.Web.Service1Behavior>
address =
binding =webHttpBinding
behaviorConfiguration =WebBehavior
bindingConfiguration =WebConfiguration
contract =沙盒.WCFUpload.Web.IService1/>
< / service>
< / services>
< /system.serviceModel>
另外在System.Web中增加了System.Web中允许的数据量
< system.web>
< otherStuff> ...< / otherStuff>
< httpRuntime maxRequestLength =2000000/>
< /system.web>
这只是基础知识,但允许添加一个Progress方法来显示ajax进度条你可能想添加一些安全性。
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.
Ta, Ant
解决方案 So, here goes...
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);
}
Create the class...
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>
Also in the System.Web increase the amount of data allowed in System.Web
<system.web>
<otherStuff>...</otherStuff>
<httpRuntime maxRequestLength="2000000"/>
</system.web>
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屋!