title: Download File from C# through Web Method via Ajax call?
body: <p>I have tried to download the file from the server through the webmethod
  but it has not work for me.
  my code as below </p>
  
  <pre><code>     [System.Web.Services.WebMethod()]
  public static string GetServerDateTime(string msg)
  {
      String result = "Result : " + DateTime.Now.ToString() + " - From Server";
      System.IO.FileInfo file = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["FolderPath"].ToString()) + "\\" + "Default.aspx");
      System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
      Response.ClearContent();
      Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
      Response.AddHeader("Content-Length", file.Length.ToString());
      Response.ContentType = "application/octet-stream";
      Response.WriteFile(file.FullName);
      //HttpContext.Current.ApplicationInstance.CompleteRequest();
      Response.Flush();
      Response.End();
      return result;        
  }
  </code></pre>
  
  <p>and my ajax call code is as below</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
      function GetDateTime() {
                      var params = "{'msg':'From Client'}";
                      $.ajax
                        ({
                            type: "POST",
                            url: "Default.aspx/GetServerDateTime",
                            data: params,
                            contentType: "application/json;charset=utf-8",
                            dataType: "json",
                            success: function (result) {
                                alert(result.d);
                            },
                            error: function (err) {
  
                            }
                        });
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>and i have called this function in button click..</p>
  
  <p>i don't know how to download the file with other methods</p>
  
  <p>Please suggest me if any other methods available or give the correction in the same code.</p>
  
  <p>Thanks to all..</p>