You can try to override any content-type and upload all files as application/octet-stream, or text/plain using an IOperationBehavior.
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);
}
}
And you would have to modify your Service contract to look like this one
[OperationContract]
[WebInvoke(Method = "POST",UriTemplate = "File/{fileName}")]
[WebContentType("application/octet-stream")]
bool UploadFile(string fileName, Stream fileContents);
Although, if you are uploading from a webpage, wouldn't the data be posted in a multipart/form-data format?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…