HTML表单POST到WC​​F服务表单、HTML、WC、POST

2023-09-04 06:20:41 作者:难再会面

有麻烦的WCF服务有一个HTML表单提交的工作。我创建一个SVGToPng服务。该服务接受一个字符串(SVG数据),并将其转换成一个图片下载(有保存文件的对话)。现在所有的现有的服务被配置为使用JSON作为消息类型。这种特殊的方法将是唯一的,而我需要执行一个良好的,老式的表单POST。

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.

下面是该服务的接口。

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

有关我在该服务只是读取现有的图像文件,并返回其为了测试(只是为了测试该服务)。这里的code。

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");

在我的JavaScript动态地创建表单,添加我所需要的值,POST的形式,然后将其从文档中删除。下面是JavaScript的。

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);

最后,这里是错误消息我收到我的WCF日志文件。

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

传入的消息有一个意外的消息格式原始。预期的消息格式的操作是的Xml','的Json'。这可能是因为一个WebContentTypeMapper尚未对结合配置。见WebContentTypeMapper的文档了解更多信息。

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.

任何帮助多少AP preciated,在此先感谢。

Any help in much appreciated, thanks in advance.

推荐答案

您的操作需要一个字符串 - 在一定的格式,它可以是一个JSON字符串(与内容类型应用程序了/ JSON)或XML封装在<字符串> 元素(与序列命名空间)和内容类型为text / xml(或应用程序/ XML)。问题是,表单POST发送表单/ URL-CN codeD数据(内容类型application / x-WWW的形式urlen codeD)。

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不支持的形式,urlen codeD外的开箱即用,但你可以得到jQuery的支持,从 HTTP://wcf.$c$cplex.com 其中有一些类来支持它,或者把输入作为一个流(像你这样的输出)和解析表格/ urlen codeD数据吧。

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.