title: Download files from file server using javascript and C#
body: <p>I have a file present in file server ( for ex: Local directory E :\ drive )</p>
  
  <p>I have a server side method to download the file </p>
  
  <pre><code>[System.Web.Services.WebMethod]
      public static void DownloadFile(string AcctNum, string OfficeCode)
      {
  
          string fName = "E:\\FILES\\STATEMENTS\\sample.pdf";
          FileInfo fi = new FileInfo(fName);
          long sz = fi.Length;
  
          HttpContext.Current.Response.ClearContent();
          if (System.IO.Path.GetFileName(fName).EndsWith(".txt"))
          {
              HttpContext.Current.Response.ContentType = "application/txt";
          }
          else if (System.IO.Path.GetFileName(fName).EndsWith(".pdf"))
          {
              HttpContext.Current.Response.ContentType = "application/pdf";
          }
          else
          {
              HttpContext.Current.Response.ContentType = "image/jpg";
          }
  
  
          HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(fName)));
          HttpContext.Current.Response.AddHeader("Content-Length", sz.ToString());
          HttpContext.Current.Response.TransmitFile(fName);
          HttpContext.Current.Response.Flush();
          HttpContext.Current.Response.End();
      }
  </code></pre>
  
  <p>I am calling this method from a javascript function as follows</p>
  
  <pre><code>function DownloadStatement(account,office) {
  
          PageMethods.DownloadFile(account,office);
  
      }
  </code></pre>
  
  <p>DownloadFile() is getting executed but file is not getting downloaded through browser.</p>
  
  <p>What am i missing here.
  Please help.</p>