title: File download in ASP.Net using C#
body: <p>I am trying to make my client to download file from my website. I tried this code:</p>
  
  <pre><code>string fName = Server.MapPath((new_exwork.FilePath));
  FileInfo fi = new FileInfo(fName);
  long sz = fi.Length;
  Response.ClearContent();
  Response.ContentType = MimeType(Path.GetExtension(fName));
  Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(fName)));
  Response.AddHeader("Content-Length", sz.ToString("F0"));
  Response.TransmitFile(fName);
  Response.End();
  </code></pre>
  
  <p>But I get the following exception:</p>
  
  <blockquote>
    <p>Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.</p>
    
    <p>MimeType function</p>
  </blockquote>
  
  <pre><code>private static string MimeType(string Extension)
      {
          string mime = "application/octetstream";
          if (string.IsNullOrEmpty(Extension))
              return mime;
  
          string ext = Extension.ToLower();
          Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
          if (rk != null &amp;&amp; rk.GetValue("Content Type") != null)
              mime = rk.GetValue("Content Type").ToString();
          return mime;
      } 
  </code></pre>