Html WCF 服务接受后编码的多部分/表单数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1354749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF service to accept a post encoded multipart/form-data
提问by Anthony Johnston
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?
有谁知道,或者最好有一个 WCF 服务的例子,它会接受一个表单后编码,multipart/form-data
即。从网页上传文件?
I have come up empty on google.
我在谷歌上空了。
Ta, Ant
塔,蚂蚁
回答by Anthony Johnston
So, here goes...
所以,这里...
Create your service contract which an operation which accepts a stream for its only parameter, decorate with WebInvoke as below
创建您的服务合同,其中一个接受流作为其唯一参数的操作,使用 WebInvoke 进行装饰,如下所示
[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 中增加 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.
这只是基础知识,但允许添加 Progress 方法来显示 ajax 进度条,您可能需要添加一些安全性。
回答by marc_s
I don't exactly know what you're trying to accomplish here, but there's no built-in support in "classic" SOAP-based WCF to capture and handle form post data. You'll have to do that yourself.
我不完全知道您要在这里完成什么,但是“经典”基于 SOAP 的 WCF 中没有内置支持来捕获和处理表单发布数据。你必须自己做。
On the other hand, if you're talking about REST-based WCF with the webHttpBinding, you could certainly have a service methods that is decorated with the [WebInvoke()] attribute which would be called with a HTTP POST method.
另一方面,如果您正在谈论带有 webHttpBinding 的基于 REST 的 WCF,您当然可以拥有一个用 [WebInvoke()] 属性修饰的服务方法,该属性将使用 HTTP POST 方法调用。
[WebInvoke(Method="POST", UriTemplate="....")]
public string PostHandler(int value)
The URI template would define the URI to use where the HTTP POST should go. You'd have to hook that up to your ASP.NET form (or whatever you're using to actually do the post).
URI 模板将定义 URI 以使用 HTTP POST 应该去的地方。您必须将它连接到您的 ASP.NET 表单(或您实际用于发布的任何表单)。
For a great introduction to REST style WCF, check out Aaron Skonnard's screen cast serieson the WCF REST Starter Kit and how to use it.
有关 REST 风格 WCF 的精彩介绍,请查看 Aaron Skonnard关于 WCF REST Starter Kit的屏幕投射系列以及如何使用它。
Marc
马克