[英]How to download stream returned from WCF service and that WCF service called from jquery ajax call
我正在使用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調用時,它將返回錯誤。 有人可以幫忙嗎?
實際上,問題在於您在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",
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.