• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

tgbaoli

好记性不如烂笔头
  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

存

Code
    public static 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;

                
double pack = 10240; //10K bytes
                
//int sleep = 200;   //每秒5次   即5*10K bytes每秒
                int sleep = (int)Math.Floor(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=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));

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

                
for (int i = 0; i < maxCount; i++)
                {
                    
if (_Response.IsClientConnected)
                    {
                        _Response.BinaryWrite(br.ReadBytes(
int.Parse(pack.ToString())));
                        Thread.Sleep(sleep);
                    }
                    
else
                    {
                        i 
= maxCount;
                    }
                }
            }
            
catch
            {
                
return false;
            }
            
finally
            {
                br.Close();

                myFile.Close();
            }
        }
        
catch
        {
            
return false;
        }
        
return true;
    }
    
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {

        
if (e.CommandName == "loadItem")
        {
            LinkButton link 
= (LinkButton)e.Item.FindControl("LinkButton1");
            
if (link != null)
            {
                
string _fileName1 = link.Text.ToString();
                
string _fileName = link.CommandArgument.ToString();
                
//string sql = "select uploads from upfile where title=" + "'" + title + "'";

                
                
string path = @"D:~\UpLoads\";
                
string _fullPath = path + _fileName;

                
bool success = ResponseFile(Page.Request, Page.Response, _fileName, _fullPath, 1024000);
                
//bool success = ResponseFile(Page.Request, Page.Response, "filename1", @"D:\tantao\UpLoads", 1024000);
                if (!success)
                    Response.Write(
"<script>alert('下载文件失败!')</script>");
            }

posted on 2008-08-12 11:40  铁山  阅读(154)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3