您可以尝试覆盖任何内容类型,并使用IOperationBehavior将所有文件上载为application/octet-stream或text/plain.
public class WebContentTypeAttribute : Attribute, IOperationBehavior, IDispatchMessageFormatter { private IDispatchMessageFormatter innerFormatter; public string ContentTypeOverride { get; set; } public WebContentTypeAttribute(string contentTypeOverride) { this.ContentTypeOverride = contentTypeOverride; } // IOperationBehavior public void Validate(OperationDescription operationDescription) { } public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) { innerFormatter = dispatchOperation.Formatter; dispatchOperation.Formatter = this; } public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) { } public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters) { } // IDispatchMessageFormatter public void DeserializeRequest(Message message, object[] parameters) { if (message == null) return; if (string.IsNullOrEmpty(ContentTypeOverride)) return; var httpRequest = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name]; httpRequest.Headers["Content-Type"] = ContentTypeOverride; } public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) { return innerFormatter.SerializeReply(messageVersion, parameters, result); } }
而且你必须修改你的服务合同,看起来像这个
[OperationContract] [WebInvoke(Method = "POST",UriTemplate = "File/{fileName}")] [WebContentType("application/octet-stream")] bool UploadFile(string fileName, Stream fileContents);
虽然,如果您从网页上传,数据不会以多部分/表格数据格式发布吗?
您可以尝试覆盖任何内容类型,并使用IOperationBehavior将所有文件上载为application/octet-stream或text/plain.
public class WebContentTypeAttribute : Attribute, IOperationBehavior, IDispatchMessageFormatter { private IDispatchMessageFormatter innerFormatter; public string ContentTypeOverride { get; set; } public WebContentTypeAttribute(string contentTypeOverride) { this.ContentTypeOverride = contentTypeOverride; } // IOperationBehavior public void Validate(OperationDescription operationDescription) { } public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) { innerFormatter = dispatchOperation.Formatter; dispatchOperation.Formatter = this; } public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) { } public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters) { } // IDispatchMessageFormatter public void DeserializeRequest(Message message, object[] parameters) { if (message == null) return; if (string.IsNullOrEmpty(ContentTypeOverride)) return; var httpRequest = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name]; httpRequest.Headers["Content-Type"] = ContentTypeOverride; } public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) { return innerFormatter.SerializeReply(messageVersion, parameters, result); } }
而且你必须修改你的服务合同,看起来像这个
[OperationContract] [WebInvoke(Method = "POST",UriTemplate = "File/{fileName}")] [WebContentType("application/octet-stream")] bool UploadFile(string fileName, Stream fileContents);
虽然,如果您从网页上传,数据不会以多部分/表格数据格式发布吗?