【发布时间】:2011-09-28 22:06:37
【问题描述】:
无法让 WCF 服务处理 HTML 表单帖子。我正在创建一个 SVGToPng 服务。该服务接受字符串(SVG 数据)并将其转换为要下载的图像(带有保存文件对话框)。现在所有现有服务都配置为使用 JSON 作为消息类型。这种特殊的方法将是独一无二的,而我需要执行一个老式的表单 POST。
这是服务的接口。
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = UriTemplate.SvgExportPng, BodyStyle = WebMessageBodyStyle.Bare)]
Stream ExportSvgToPng(String svgData);
为了测试,我让服务读取现有图像文件并将其返回(只是为了测试服务)。这是代码。
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 中,我动态创建表单,添加所需的值,发布表单,然后将其从文档中删除。这是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 日志文件中收到的错误消息。
传入的消息具有意外的消息格式“原始”。该操作的预期消息格式为“Xml”、“Json”。这可能是因为尚未在绑定上配置 WebContentTypeMapper。详情请参阅 WebContentTypeMapper 的文档。
任何帮助都非常感谢,在此先感谢。
【问题讨论】:
标签: javascript .net wcf http-post wcf-rest