| 网站首页 | 硬件维修 | 应用学院 | 网络组建 | 网站制作 | 菜鸟黑客 | 编程之道 | 数码大全 | 娱乐休闲 | 软件下载 | 在线视频 | 请您留言 | 技术论坛 | 
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
  • javascript模拟游戏中的…

  • javascript制作闪烁的边…

  • javascript制作浮动的工…

  • javascript设计漫天雪花

  • javascript设计网页中的…

  • 用javascript实现利用FL…

  • javascript实例教程(20)…

  • javascript实例教程(20)…

  • javascript实例教程(20)…

  • javascript实例教程(20)…

  • Q
    您现在的位置: 我是IT人 >> 网站制作 >> ASP.NET >> 文章正文
    ASP.NET提供文件下载函数         
    ASP.NET提供文件下载函数
    作者:网络 文章来源:转载 点击数: 更新时间:2005-8-10
    [ 字体:缩小 正常 放大 | 双击自动滚屏 ]
    请选择合适的字体颜色:

      // 输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小
      // 输入参数 _Request: Page.Request对象,  _Response: Page.Response对象, _fileName: 下载文件名, _fullPath: 带文件名下载路径, _speed 每秒允许下载的字节数
      // 返回是否成功
      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;
        
         int 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;2004129201742.htm=" + 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(pack));
           Thread.Sleep(sleep);
          }
          else
          {
           i=maxCount;
          }
         }
        }
        catch
        {
         return false;
        }
        finally
        {
         br.Close();
         myFile.Close();
        }
       }
       catch
       {
        return false;
       }
       return true;
      }

    [1] [2] 下一页  

    文章录入:小秦    责任编辑:小秦 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    | 设为首页 | 加入收藏 | 联系站长 | 关于我们 | 友情链接 | 版权申明 |