I have an xml formatted structure in hidden field.I have to make a datatable from that and download that.
Now to call generic handler,I can't use asp button.If I then my table structure which is in that page will be destroyed.So I'm calling the handler as
$.ajax({
url:"download.ashx",
data: { xmlstruct: escape(xmldataset), filename: filename },
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function () {
btn.text("Downloading...");
},
success: function (response) {
btn.text(btntxt);
if (response.d == "0") {
alert("Downloaded");
}
else {
alert("Can not be downloaded");
}
btn.prop("disabled", false);
},
failure: function (response) {
btn.text(btntxt);
alert(response.d);
btn.prop("disabled", false);
}
});
The handler is receiving data and I can create datatable from the xmlstruct parameter value.
public void ProcessRequest (HttpContext context) {
string filename = context.Request["filename"];
string xmlstruct = Uri.UnescapeDataString(context.Request["xmlstruct"]);
DataSet ds = new DataSet();
ds.ReadXml(new System.Xml.XmlTextReader(new System.IO.StringReader(xmlstruct)));
DataTable dt = new DataTable();
if (ds.Tables.Count > 0)
{
dt = ds.Tables[0];
ds.Dispose();
getdownloadelement(dt, filename,context);
}
dt.Dispose();
}
private void getdownloadelement(DataTable dt, string filename,HttpContext context)
{
context.Response.Buffer = true;
context.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", filename));
context.Response.ContentType = "application/ms-excel";
string str = string.Empty;
foreach (DataColumn dtcol in dt.Columns)
{
context.Response.Write(str + dtcol.ColumnName);
str = "\t";
}
context.Response.Write("\n");
foreach (DataRow dr in dt.Rows)
{
str = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
context.Response.Write(str + Convert.ToString(dr[j]));
str = "\t";
}
context.Response.Write("\n");
}
context.Response.End();
}
There is no error,but can't download the file.