有谁知道或更佳的WCF服务示例,该服务将接受表单后编码的multipart/form-data即。从网页上传文件?
multipart/form-data
我在Google上空了。
所以,这里…
创建您的服务合同,该操作将接受流作为其唯一参数的操作,并使用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进度栏,并且您可能需要增加一些安全性。