您好我需要在服务器端生成一些文件并使用 AJAX 将它们返回给客户端
我在服务器(ASHX)上创建下一个代码
public void ProcessRequest(HttpContext context)
{
string dataViewID = context.Request.Form["dataViewID"];
MyService service = new MyService();
var data = service.GetStores(int.Parse(dataViewID), "", null);
IMyExportService exportservice = new MyExportService();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "export.cvs");
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms))
{
exportservice.ExportTo("csv", sw, data);
ms.Position = 0;
HttpContext.Current.Response.Write(ms.ToArray());
}
}
}
在客户端我创建下一个代码: $("#btnexport").click(function () { var paramData = { "dataViewID": 1524129, "filter": "", extent: null }; //full map $.ajax ({ url: '/marketVuePortal/'+'FileExport.ashx', type: 'POST',
dataType: "json", data: {dataViewID:1524129},
success: function (result) { //这里应该是什么? },错误:函数(xhr){警报(“错误”);}}
)}
);
但是我有 2 个问题,我不知道为什么,但我总是出错,但在调试时所有代码都运行良好。第二个是我不知道怎么说浏览器他需要保存页面而不重新加载。