【问题标题】:How to download stream returned from WCF service and that WCF service called from jquery ajax call如何下载从 WCF 服务返回的流以及从 jquery ajax 调用调用的 WCF 服务
【发布时间】:2014-03-14 20:43:18
【问题描述】:

我正在使用 JQuery 从客户端使用 WCF 服务方法。我想在客户端保存从 WCF 方法返回的流数据。以下 WCF 方法运行良好,其中我使用我们自己的 excelExport 方法创建内存流 -

public System.IO.Stream TestExportToExcel()
    {
        using (_oxBowData = new Data.OxbowDataContext())
        {
            ExcelExport excelExport = new ExcelExport();
            List<SheetData> sheets = new List<SheetData>();
            sheets.Add(new SheetData()
            {
                SheetName = "sheet1",
                DataList = _oxBowData.GetLunchReportData(new DateTime(2013, 12, 24)).ToList<object>()
            });
            using (MemoryStream xlsstream = excelExport.ExportToExcelToStream(sheets))
            {
                xlsstream.Position = 0L;
                return xlsstream;
            }
        }
    }

服务合同:

[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "TestExportToExcel")]
System.IO.Stream TestExportToExcel();

以下是我在客户端使用的,但它只是返回错误-

$.ajax({
                type: "GET",
                url: u + "/Terminal/ManagementService.svc/TestExportToExcel",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                processData: false,
                success: function (data) {
                    alert(JSON.stringify(data));
                },
                error: function (data) {
                    alert(JSON.stringify(data));
                }
            });

当我调用这个客户端 ajax 调用时,它会返回错误。有人可以帮忙吗?

【问题讨论】:

标签: c# jquery ajax json wcf


【解决方案1】:

实际上,问题在于您在 JSON 中指定 RequestFormat = WebMessageFormat.Json 而您的方法 System.IO.Stream TestExportToExcel() 没有任何参数来接收它。

此外!如果您使用 POST 方法,.NET 会强制您指定 RequestFormat。所以,像这样改变你的合同。

您的服务合同。

[OperationContract]
[WebInvoke(Method = "POST", 
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare, 
ResponseFormat = WebMessageFormat.Json, 
UriTemplate   = "TestExportToExcel")]
System.IO.Stream TestExportToExcel(MyCustomObject jsonRequest);

阿贾克斯:

type: "POST",

【讨论】:

  • Chaning Get to Post 没有帮助。
猜你喜欢
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多