Skip to content
Loading
 
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. const string fName = @"C:\picture.bmp";
  4. FileInfo fi = new FileInfo(fName);
  5. long sz = fi.Length;
  6. Response.ClearContent();
  7. Response.ContentType = MimeType(Path.GetExtension(fName));
  8. Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}",System.IO.Path.GetFileName(fName)));
  9. Response.AddHeader("Content-Length", sz.ToString("F0"));
  10. Response.TransmitFile(fName);
  11. Response.End();
  12. }

  13. public static string MimeType(string Extension)
  14. {
  15. string mime = "application/octetstream";
  16. if (string.IsNullOrEmpty(Extension))
  17. return mime;
  18. string ext = Extension.ToLower();
  19. Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
  20. if (rk != null && rk.GetValue("Content Type") != null)
  21. mime = rk.GetValue("Content Type").ToString();
  22. return mime;
  23. }
+2