Click here to Skip to main content
14,736,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to export file using Web Method and JQuery AJAX. Web method of file downloading individually working fine but when I am consuming service method in AJAX it is not working
code----

[WebMethod]
    public  String ExportToExcel()
    {
        try
        {
            // DataSet ds;
            HttpResponse response = HttpContext.Current.Response;
            // first let's clean up the response.object
            response.Clear();
            response.Charset = "";
            response.ContentEncoding = System.Text.Encoding.Default;

            // set the response mime type for excel
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=\"ExportTest.xls\"");

            // create a string writer
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    response.Write(sw.ToString());
                    response.End();
                }
            }
            return "Exported";
        }
        catch (Exception ex) {
            return ex.Message;
        }
    }


Javascript calling:
$.ajax({
              url:"WebService.asmx?op=ExportToExcel"
          , type: "GET"
          ,contentType: "application/vnd.ms-excel"
          , data: {}
          , success: function (res) {
              alert("Success");
          }
          , error: function (res) {
              alert("Error");
          }
      });
Posted
Updated 18-Aug-14 20:33pm
v2

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