夜隼

RYSZ

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

FileStream stream = new FileStream(path, FileMode.Open); 
   Response.ContentType = "application/octet-stream";
   Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8));
   
   //开始下载文件,注意这里下载必须1024*1024个字节的读,不能一次读入内存,否则服务器必死
   try
   {
    int nowLength = 0;
    int maxLengthPerPackage = 1024*1024;
    byte[] by;
    for(;nowLength < stream.Length;)
    {
     by = new byte[maxLengthPerPackage];
     int canReadLength = stream.Read(by, 0, by.Length); // 读取文件内容
     //this.SocketSendText(by,false,0,canReadLength);
     Response.BinaryWrite(by);
     nowLength += maxLengthPerPackage;
    }
   }
   finally
   {
    if (stream != null)
    {
     stream.Close();
    }
   }

posted on 2006-06-21 10:15  夜隼  阅读(606)  评论(0编辑  收藏  举报