Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
$.ajax({
               //type: "POST",
               //url: "uploadRes.aspx/downloadExcel",
               url:"download.ashx",
               //data: JSON.stringify({ xmlstruct: xmldataset, filename: filename }),
               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.

C#
 public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
       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);
            //ExportToExcel(dt, context); 
        }
        dt.Dispose();
        
    }

private void getdownloadelement(DataTable dt, string filename,HttpContext context)
    {
       // context.Response.ClearContent();
        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.
Posted
Comments
TheKarateKid 18-Feb-15 14:00pm    
May I suggest something?
1. You can keep your Table Structure (DataTable) in ViewState, so to retain it after postback
2. You can use ASP:Button for providing Download functionality.
3. If you are using Download Button inside the UpdatePanel then make sure you register the Download button as FullPostBackControl using ScriptManager instance.
4. Write the code in Button click to invoke the helper method or private method getdownloadelement.

HTH,
Thanks,
souvikcode 19-Feb-15 2:42am    
I have used webmethod. In that method the xml structure of a datatable is stored in session and a javascript function called window.redirect to another page.In that page I used the session to get xml,create datatable and then create excel to download.After downloading the 2nd page closed automatically.In this way there is no affect on my original page in which javascript table structure is created.So without post back on this page,I've done using a downloader page.

1 solution

I have used webmethod. In that method the xml structure of a datatable is stored in session and a javascript function called window.redirect to another page.In that page I used the session to get xml,create datatable and then create excel to download.After downloading the 2nd page closed automatically.In this way there is no affect on my original page in which javascript table structure is created.So without post back on this page,I've done using a downloader page.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900