asp.net文件下载

这个是比较好用的下载文件的代码,可以下载大文件,资源占用小,可以限速,但是如果限速了就容易死机(在线的人多时),取之于网络
/// <summary>
    
/// 输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小
    
/// </summary>
    
/// <param name="Request">Page.Request对象</param>
    
/// <param name="Response">Page.Response对象</param>
    
/// <param name="fileName">下载文件名</param>
    
/// <param name="fullPath">带文件名下载路径</param>
    
/// <param name="speed">每秒允许下载的字节数</param>
    
/// <returns>返回是否成功</returns>

    public  bool ResponseFile(HttpRequest Request, HttpResponse Response, string fileName, string fullPath, long speed)
    
{
        
try
        
{
            FileStream myFile 
= new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader br 
= new BinaryReader(myFile);
            
try
            
{
                Response.AddHeader(
"Accept-Ranges""bytes");
                Response.Buffer 
= false;
                
long fileLength = myFile.Length;
                
long startBytes = 0;

                
int pack = 1024//1K bytes
                
//int sleep = 200;   //每秒5次   即5*10K bytes每秒
                int sleep = (int)Math.Floor((decimal)1000 * pack / speed) + 1;
                
if (Request.Headers["Range"!= null)
                
{
                    Response.StatusCode 
= 206;
                    
string[] range = Request.Headers["Range"].Split(new char[] '=''-' });
                    startBytes 
= Convert.ToInt64(range[1]);
                }

                Response.AddHeader(
"Content-Length", (fileLength - startBytes).ToString());
                
if (startBytes != 0)
                
{
                    Response.AddHeader(
"Content-Range"string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
                }

                Response.AddHeader(
"Connection""Keep-Alive");
                Response.ContentType 
= "application/octet-stream";
                Response.AddHeader(
"Content-Disposition""attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName));

                br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                
int maxCount = (int)Math.Floor((decimal)(fileLength - startBytes) / pack) + 1;

                
for (int i = 0; i < maxCount; i++)
                
{
                    
if (Response.IsClientConnected)
                    
{
                        Response.BinaryWrite(br.ReadBytes(pack));
                        
//Thread.Sleep(sleep);//有问题,容易导致死机状态,只有设置了这里才可以限速,但在线人多时容易死机
                    }

                    
else
                    
{
                        i 
= maxCount;
                    }

                }

            }

            
catch
            
{
                
return false;
            }

            
finally
            
{
                br.Close();
                myFile.Close();
            }

        }

        
catch
        
{
            
return false;
        }

        
return true;
    }
posted on 2008-01-22 08:30  ringwang  阅读(381)  评论(0编辑  收藏  举报