Hi I need to generate some files on server side and return them to client with AJAX
I create next code on server (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());
}
}
}
on client i create next code:
$(“#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) {
//what should be here ?
},
error: function (xhr) {
alert(“error”);
}
}
)}
);
But I have 2 problem I don’t know why but I always get error but in debug all code working well. and second is that I don’t know how to say browser that he need save page with out reload.
This is solution that I have found.