protected void Button1_Click(object sender, EventArgs e)
  {

  1.   /*
  2.   微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
  3.   下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
  4.   代码如下:
  5.   */
  6.   Response.ContentType = "application/x-zip-compressed";
  7.   Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
  8.   string filename = Server.MapPath("DownLoad/aaa.zip");
  9.   Response.TransmitFile(filename);

  }

//WriteFile实现下载
  protected void Button2_Click(object sender, EventArgs e)
  {

  1.   /*
  2.   using System.IO;    
  3.   */
  4.   string fileName ="aaa.zip";//客户端保存的文件名
  5.   string filePath=Server.MapPath("DownLoad/aaa.zip");//路径
  6.   FileInfo fileInfo = new FileInfo(filePath);
  7.   Response.Clear();
  8.   Response.ClearContent();
  9.   Response.ClearHeaders();
  10.   Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
  11.   Response.AddHeader("Content-Length", fileInfo.Length.ToString());
  12.   Response.AddHeader("Content-Transfer-Encoding", "binary");
  13.   Response.ContentType = "application/octet-stream";
  14.   Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
  15.   Response.WriteFile(fileInfo.FullName);
  16.   Response.Flush();
  17.   Response.End();

  }

 //WriteFile分块下载
  protected void Button3_Click(object sender, EventArgs e)
  {

  1.   string fileName = "aaa.zip";//客户端保存的文件名
  2.   string filePath = Server.MapPath("DownLoad/aaa.zip");//路径
  3.   System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
  4.   if (fileInfo.Exists == true)
  5.   {
  6.   const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
  7.   byte[] buffer = new byte[ChunkSize];
  8.   Response.Clear();
  9.   System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
  10.   long dataLengthToRead = iStream.Length;//获取下载的文件总大小
  11.   Response.ContentType = "application/octet-stream";
  12.   Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
  13.   while (dataLengthToRead > 0 && Response.IsClientConnected)  
  14.   {
  15.   int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
  16.   Response.OutputStream.Write(buffer, 0, lengthRead);
  17.   Response.Flush();
  18.   dataLengthToRead = dataLengthToRead - lengthRead;
  19.   }
  20.   Response.Close();
  21.   }

  }

 //流方式下载
  protected void Button4_Click(object sender, EventArgs e)
  {

  1.   string fileName = "aaa.zip";//客户端保存的文件名
  2.   string filePath = Server.MapPath("DownLoad/aaa.zip");//路径
  3.   //以字符流的形式下载文件
  4.   FileStream fs = new FileStream(filePath, FileMode.Open);
  5.   byte[] bytes = new byte[(int)fs.Length];
  6.   fs.Read(bytes, 0, bytes.Length);
  7.   fs.Close();
  8.   Response.ContentType = "application/octet-stream";
  9.   //通知浏览器下载文件而不是打开
  10.   Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
  11.   Response.BinaryWrite(bytes);
  12.   Response.Flush();
  13.   Response.End();

  } 

 

转载原文:http://www.cnblogs.com/weixing/archive/2012/02/27/2369567.html

 

posted on 2014-11-03 16:39  lizihong  阅读(121)  评论(0编辑  收藏  举报