有没有人知道,或者更好的是有一个 WCF 服务的例子,它将接受一个表单后编码 multipart/form-data
即。从网页上传文件?
我在谷歌上空空如也。
Ant , Ant
最佳答案
所以,这里...
创建您的服务合约,其中一个操作接受流作为其唯一参数,用 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 进度条,您可能希望增加一些安全性。
关于html - WCF 服务接受后编码的多部分/表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1354749/