protected   void  Button1_Click( object  sender, EventArgs e)
  2 
      {
  3 
          // TransmitFile实现下载 
  4 
 
  5 
          /* 
  6 
          微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
  7 
          下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
  8 
          代码如下:
  9 
          */ 
 10 

 11 
         Response.ContentType  =   " application/x-zip-compressed " ;
 12 
         Response.AddHeader( " Content-Disposition " ,  " attachment;filename=keji.rar " );
 13 
          string  filename  =  Server.MapPath( " keji.rar " );
 14 
         Response.TransmitFile(filename);
 15 
         Response.Write( " <script language=/ " javascript/ "  type=/ " text / javascript/ " > " );
 16 
         Response.Write( " alert(/ " 下载成功/ " ); " );
 17 
         Response.Write( " window.location.href=/ " C_SC.aspx/ " ; " );
 18 
         Response.Write( " </script> " );
 19 

 20 
     } 
 21 
      protected   void  Button2_Click( object  sender, EventArgs e)
 22 
      {
 23 

 24 
          // WriteFile实现下载 
 25 
          string  fileName  =   " ceshi.rar " ; // 客户端保存的文件名 
 26 
          string  filePath  =  Server.MapPath( " keji.rar " ); // 路径 
 27 
 
 28 
         FileInfo fileInfo  =   new  FileInfo(filePath);
 29 
         Response.Clear();
 30 
         Response.ClearContent();
 31 
         Response.ClearHeaders();
 32 
         Response.AddHeader( " Content-Disposition " ,  " attachment;filename= "   +  fileName);
 33 
         Response.AddHeader( " Content-Length " , fileInfo.Length.ToString());
 34 
         Response.AddHeader( " Content-Transfer-Encoding " ,  " binary " );
 35 
         Response.ContentType  =   " application/octet-stream " ;
 36 
         Response.ContentEncoding  =  System.Text.Encoding.GetEncoding( " gb2312 " );
 37 
         Response.WriteFile(fileInfo.FullName);
 38 
         Response.Flush();
 39 
         Response.End();
 40 

 41 
     } 
 42 
      protected   void  Button3_Click( object  sender, EventArgs e)
 43 
      {
 44 
          // WriteFile分块下载 
 45 
 
 46 
          string  fileName  =   " GhostXP.iso " ; // 客户端保存的文件名 
 47 
          string  filePath  =  Server.MapPath( " GhostXP7.7.iso " ); // 路径 
 48 
 
 49 
          System.IO.FileInfo fileInfo  =   new  System.IO.FileInfo(filePath);
 50 

 51 
          if  (fileInfo.Exists  ==   true )
 52 
           {
 53 
              const   long  ChunkSize  =   409600 ; // 100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 
 54 
              byte [] buffer  =   new   byte [ChunkSize];
 55 

 56 
              Response.Clear();
 57 
              System.IO.FileStream iStream  =  System.IO.File.OpenRead(filePath);
 58 
              long  dataLengthToRead  =  iStream.Length; // 获取下载的文件总大小 
 59 
              Response.ContentType  =   " application/octet-stream " ;
 60 
              Response.AddHeader( " Content-Disposition " ,  " attachment; filename= "   +  HttpUtility.UrlEncode(fileName));
 61 
              while  (dataLengthToRead  >   0   &&  Response.IsClientConnected)
 62 
               {
 63 
                  int  lengthRead  =  iStream.Read(buffer,  0 , Convert.ToInt32(ChunkSize)); // 读取的大小 
 64 
                  Response.OutputStream.Write(buffer,  0 , lengthRead);
 65 
                  Response.Flush();
 66 
                  dataLengthToRead  =  dataLengthToRead  -  lengthRead;
 67 
              } 
 68 
              Response.Close();
 69 
          } 
 70 
      } 
 71 

 72 
      // 字符流方式下载文件 
 73 
      protected   void  Button4_Click( object  sender, EventArgs e)
 74 
      {
 75 
          string  fileName  =   " ce2.rar " ; // 客户端保存的文件名 
 76 
          string  filePath  =  Server.MapPath( " keji.rar " ); // 路径
 77 

 78 
          // 以字符流的形式下载文件 
 79 
         FileStream fs  =   new  FileStream(filePath, FileMode.Open);
 80 
          byte [] bytes  =   new   byte [( int )fs.Length];
 81 
         fs.Read(bytes,  0 , bytes.Length);
 82 
         fs.Close();
 83 
         Response.ContentType  =   " application/octet-stream " ;
 84 
          // 通知浏览器下载文件而不是打开 
 85 
         Response.AddHeader( " Content-Disposition " ,  " attachment;   filename= "   +  HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
 86 
         Response.BinaryWrite(bytes);
 87 
         Response.Flush();
 88 
         Response.End();
 89 

 90 
     }