HTML form POST to WCF service

余生颓废 提交于 2019-12-23 05:23:10

问题


Having trouble getting a WCF service to work with a HTML form post. I'm creating an SVGToPng service. The service accepts a String (SVG data) and converts it into an image to download (with save file dialogue). Right now all of the existing services are configured to use JSON as the message type. This particular method will be unique whereas I need to perform a good-old-fashioned form POST.

Here's the interface for the service.

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = UriTemplate.SvgExportPng, BodyStyle = WebMessageBodyStyle.Bare)]
Stream ExportSvgToPng(String svgData);

For the sake of testing I'm having the service just read an existing image file and return it (just to test the service). Here's the code.

WebOperationContext.Current.OutgoingResponse.ContentType = "image/png";
WebOperationContext.Current.OutgoingResponse.Headers.Clear();
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "attachment; filename=export-" + DateTime.Now.ToString("MM-dd-yyyy") + ".png");

return File.OpenRead(@"C:\tmp.png");

In my javascript I dynamically create the form, add the values I need, POST the form, then remove it from the document. Here's the javascript.

form.setAttribute("method", "POST");
form.setAttribute("action", Daedalus.Current.WcfUrl + '/svg/png');

hiddenField.setAttribute("name", "svgData");
hiddenField.setAttribute("value", view.trend.getSVG());

form.appendChild(hiddenField);
document.body.appendChild(form);

form.submit();
document.body.removeChild(form);

Finally here is the error message I'm receiving in my WCF log files.

The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.

Any help in much appreciated, thanks in advance.


回答1:


Your operation expects a string - and in a certain format, which is either a JSON string (with content type applicaiton/json) or as XML wrapped in a <string> element (with the serialization namespace) and content type text/xml (or application/xml). The problem is that the form POST is sending forms/url-encoded data (content type application/x-www-form-urlencoded).

WCF doesn't support forms-urlencoded out-of-the-box, but you can either get the "jQuery support" from http://wcf.codeplex.com which has some classes to support it, or take the input as a Stream (like you do with the output) and parse the forms/urlencoded data yourself.



来源:https://stackoverflow.com/questions/6584939/html-form-post-to-wcf-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!