<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Option Explicit%>

<%
'================================================================
'
'        带进度条的ASP无组件断点续传下载
'
'================================================================
'简介:
'  1)利用xmlhttp方式
'  2)无组件
'  3)异步方式获取,节省服务器资源
'  4)服务器到服务器的文件传送。(当然,你自己电脑上的IIS也是http服务器)
'  5)支持断点续传
'  6)分段下载
'  7)使用缓冲区,提升下载速度
'  8)支持大文件下载(速度我就不说了,你可以测,用事实说话)
'  9)带进度条:下载百分比、下载量、即时下载速度、平均下载速度
'
'用法:
'  设置好下面的三个变量,RemoteFileUrl、LocalFileUrl、RefererUrl
'
'作者:午夜狂龙(Madpolice)
'madpolice_dong@163.com
'2005.12.25
'================================================================
%>

<%'-----------------------------以下为设置部分--------------------------------%>
<%Server.Scripttimeout = 24 * 60 * 60  '脚本超时设置,这里设为24小时%>
<%
Dim RemoteFileUrl  '远程文件路径
Dim LocalFileUrl  '本地文件路径,相对路径,可以包含/及..

'速度问题注意:下面这个测试文件是在“网通”服务器上!!!
RemoteFileUrl = "http://hdt.driversky.com/down/foxmail60beta2.exe"
LocalFileUrl = "foxmail60beta2.exe"

Dim RefererUrl
'该属性设置文件下载的引用页,
'某些网站只允许通过他们网站内的连接下载文件,
'这些网站的服务器判断用户是否是在他们网站内点击的文件链接就是靠这个属性。
RefererUrl = "http://www.skycn.com/crack_skycn.html"  '若远程服务器未限制,可留空

Dim BlockSize  '分段下载的块大小
Dim BlockTimeout  '下载块的超时时间(秒)

BlockSize = 128 * 1024  '128K,按1M带宽计算的每秒下载量(可根据自己的带宽设置,带宽除以8),建议不要设的太小
BlockTimeout = 64  '应当根据块的大小来设置。这里设为64秒。如果128K的数据64秒还下载不完(按每秒2K保守估算),则超时。

Dim PercentTableWidth  '进度条总宽度

PercentTableWidth = 560
%>
<%'-----------------------------以上为设置部分--------------------------------%>

<%
'***********************************************************************
'          !!!以下内容无须修改!!!
'***********************************************************************
%>
<%
Dim LocalFileFullPhysicalPath  '本地文件在硬盘上的绝对路径

LocalFileFullPhysicalPath = Server.Mappath(LocalFileUrl)
%>

<%
Dim http,ados

On Error Resume Next
Set http = Server.CreateObject("Msxml2.ServerXMLHTTP.7.0")
If Err Then
  Err.Clear

Set http = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
  If Err Then
    Err.Clear

Set http = Server.CreateObject("Msxml2.ServerXMLHTTP.5.0")
    If Err Then
      Err.Clear

Set http = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
      If Err Then
        Err.Clear

Set http = Server.CreateObject("Msxml2.ServerXMLHTTP")
        If Err Then
          Err.Clear
          Response.Write "服务器不支持Msxml,本程序无法运行!"
          Response.End
        End If
      End If
    End If
  End If
End If
On Error Goto 0

Set ados = Server.CreateObject("Adodb.Stream")
%>

<%
Dim RangeStart  '分段下载的开始位置
Dim fso

Set fso = Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(LocalFileFullPhysicalPath) Then  '判断要下载的文件是否已经存在
RangeStart = fso.GetFile(LocalFileFullPhysicalPath).Size  '若存在,以当前文件大小作为开始位置
Else
RangeStart = 0  '若不存在,一切从零开始
fso.CreateTextFile(LocalFileFullPhysicalPath).Close  '新建文件
End If
Set fso = Nothing
%>

<%
Dim FileDownStart  '本次下载的开始位置
Dim FileDownEnd  '本次下载的结束位置
Dim FileDownBytes  '本次下载的字节数
Dim DownStartTime  '开始下载时间
Dim DownEndTime  '完成下载时间
Dim DownAvgSpeed  '平均下载速度

Dim BlockStartTime  '块开始下载时间
Dim BlockEndTime  '块完成下载时间
Dim BlockAvgSpeed  '块平均下载速度

Dim percentWidth  '进度条的宽度
Dim DownPercent  '已下载的百分比

FileDownStart = RangeStart
%>

<%
Dim adosCache  '数据缓冲区
Dim adosCacheSize  '缓冲区大小

Set adosCache = Server.CreateObject("Adodb.Stream")
adosCache.Type = 1  '数据流类型设为字节
adosCache.Mode = 3  '数据流访问模式设为读写
adosCache.Open
adosCacheSize = 4 * 1024 * 1024  '设为4M,获取的数据先放到(内存)缓冲区中,当缓冲区满的时候数据写入磁盘

'若在自己的电脑上运行本程序,当下载百兆以上级别的大文件的时候,可设置大的缓冲区
'当然,也不要设的太大,免得发生(按下浏览器上的停止按钮或断电等)意外情况导致缓冲区中的数据没有存盘,那缓冲区中的数据就白下载了
%>

<%
'先显示html头部
Response.Clear
Call HtmlHead()
Response.Flush
%>

<%
Dim ResponseRange  '服务器返回的http头中的"Content-Range"
Dim CurrentLastBytes  '当前下载的结束位置(即ResponseRange中的上限)
Dim TotalBytes  '文件总字节数
Dim temp

'分段下载
DownStartTime = Now()

Do
  BlockStartTime = Timer()

http.open "GET",RemoteFileUrl,true,"",""  '用异步方式调用serverxmlhttp

'构造http头
  http.setRequestHeader "Referer",RefererUrl
  http.setRequestHeader "Accept","*/*"
  http.setRequestHeader "User-Agent","Baiduspider+(+http://www.baidu.com/search/spider.htm)"  '伪装成Baidu
  'http.setRequestHeader "User-Agent","Googlebot/2.1 (+http://www.google.com/bot.html)"  '伪装成Google
  http.setRequestHeader "Range","bytes=" & RangeStart & "-" & Cstr(RangeStart + BlockSize - 1)  '分段关键
  http.setRequestHeader "Content-Type","application/octet-stream"
  http.setRequestHeader "Pragma","no-cache"
  http.setRequestHeader "Cache-Control","no-cache"

http.send  '发送

'循环等待数据接收
  While (http.readyState <> 4)
    '判断是否块超时
    temp = Timer() - BlockStartTime
    If (temp > BlockTimeout) Then
      http.abort
      Response.Write "<script>document.getElementById(""status"").innerHTML=""<strong>错误:数据下载超时,建议重试。</strong>"";</script>" & vbNewLine & "</body></html>"
      Call ErrHandler()
      Call CloseObject()
      Response.End
    End If

http.waitForResponse 1000  '等待1000毫秒
  Wend

'检测状态
  If http.status = 416 Then  '服务器不能满足客户在请求中指定的Range头。应当是已下载完毕。
    FileDownEnd = FileDownStart  '设置一下FileDownEnd,免得后面的FileDownBytes计算出错
    Call CloseObject()
    Exit Do
  End If

'检测状态
  If http.status > 299 Then  'http出错
    Response.Write "<script>document.getElementById(""status"").innerHTML=""<strong>http错误:" & http.status & "&nbsp;" & http.statusText & "</strong>"";</script>" & vbNewLine & "</body></html>"
    Call ErrHandler()
    Call CloseObject()
    Response.End
  End If

'检测状态
  If http.status <> 206 Then  '服务器不支持断点续传
    Response.Write "<script>document.getElementById(""status"").innerHTML=""<strong>错误:服务器不支持断点续传!</strong>"";</script>" & vbNewLine & "</body></html>"
    Call ErrHandler()
    Call CloseObject()
    Response.End
  End If

'检测缓冲区是否已满
  If adosCache.Size >= adosCacheSize Then
    '打开磁盘上的文件
    ados.Type = 1  '数据流类型设为字节
    ados.Mode = 3  '数据流访问模式设为读写
    ados.Open
    ados.LoadFromFile LocalFileFullPhysicalPath  '打开文件
    ados.Position = ados.Size  '设置文件指针初始位置

'将缓冲区数据写入磁盘文件
    adosCache.Position = 0
    ados.Write adosCache.Read
    ados.SaveToFile LocalFileFullPhysicalPath,2  '覆盖保存
    ados.Close

'缓冲区复位
    adosCache.Position = 0
    adosCache.SetEOS
  End If
  
  '保存块数据到缓冲区中
  adosCache.Write http.responseBody  '写入数据

'判断是否全部(块)下载完毕
  ResponseRange = http.getResponseHeader("Content-Range")  '获得http头中的"Content-Range"
  If ResponseRange = "" Then  '没有它就不知道下载完了没有
    Response.Write "<script>document.getElementById(""status"").innerHTML=""<strong>错误:文件长度未知!</strong>"";</script>" & vbNewLine & "</body></html>"
    Call CloseObject()
    Response.End
  End If
  temp = Mid(ResponseRange,Instr(ResponseRange,"-")+1)  'Content-Range是类似123-456/789的样子
  CurrentLastBytes = Clng(Left(temp,Instr(temp,"/")-1))  '123是开始位置,456是结束位置
  TotalBytes = Clng(Mid(temp,Instr(temp,"/")+1))  '789是文件总字节数
  If TotalBytes - CurrentLastBytes = 1 Then
    FileDownEnd = TotalBytes

'将缓冲区数据写入磁盘文件
    ados.Type = 1  '数据流类型设为字节
    ados.Mode = 3  '数据流访问模式设为读写
    ados.Open
    ados.LoadFromFile LocalFileFullPhysicalPath  '打开文件
    ados.Position = ados.Size  '设置文件指针初始位置
    adosCache.Position = 0
    ados.Write adosCache.Read
    ados.SaveToFile LocalFileFullPhysicalPath,2  '覆盖保存
    ados.Close

Response.Write "<script>document.getElementById(""downsize"").innerHTML=""" & TotalBytes & """;</script>" & vbNewLine
    Response.Flush
    Call CloseObject()
    Exit Do  '结束位置比总大小少1就表示传输完成了
  End If
  
  '调整块开始位置,准备下载下一个块
  RangeStart = RangeStart + BlockSize

'计算块下载速度、进度条宽度、已下载的百分比
  BlockEndTime = Timer()
  temp = (BlockEndTime - BlockStartTime)
  If temp > 0 Then
    BlockAvgSpeed = Int(BlockSize / 1024 / temp)
  Else
    BlockAvgSpeed = ""
  End If
  percentWidth = Int(PercentTableWidth * RangeStart / TotalBytes)
  DownPercent = Int(100 * RangeStart / TotalBytes)

'更新进度条
  Response.Write "<script>document.getElementById(""downpercent"").innerHTML=""" & DownPercent & "%"";document.getElementById(""downsize"").innerHTML=""" & RangeStart & """;document.getElementById(""totalbytes"").innerHTML=""" & TotalBytes & """;document.getElementById(""blockavgspeed"").innerHTML=""" & BlockAvgSpeed & """;document.getElementById(""percentdone"").style.width=""" & percentWidth & """;</script>" & vbNewLine
  Response.Flush
Loop While Response.IsClientConnected

If Not Response.IsClientConnected Then
  Response.End
End If

DownEndTime = Now()
FileDownBytes = FileDownEnd - FileDownStart
temp = DateDiff("s",DownStartTime,DownEndTime)
If (FileDownBytes <> 0) And (temp <> 0) Then
  DownAvgSpeed = Int((FileDownBytes / 1024) / temp)
Else
  DownAvgSpeed = ""
End If

'全部下载完毕后更新进度条
Response.Write "<script>document.getElementById(""downpercent"").innerHTML=""100%"";document.getElementById(""percentdone"").style.width=""" & PercentTableWidth & """;document.getElementById(""percent"").style.display=""none"";document.getElementById(""status"").innerHTML=""<strong>下载完毕!用时:" & S2T(DateDiff("s",DownStartTime,DownEndTime)) & ",平均下载速度:" & DownAvgSpeed & "K/秒</strong>"";</script>" & vbNewLine
%>

</body>
</html>

<%
Sub CloseObject()
  Set ados = Nothing
  Set http = Nothing
  adosCache.Close
  Set adosCache = Nothing
End Sub
%>

<%
'http异常退出处理代码
Sub ErrHandler()
  Dim fso

Set fso = Server.CreateObject("Scripting.FileSystemObject")
  If fso.FileExists(LocalFileFullPhysicalPath) Then  '判断要下载的文件是否已经存在
    If fso.GetFile(LocalFileFullPhysicalPath).Size = 0 Then  '若文件大小为0
      fso.DeleteFile LocalFileFullPhysicalPath  '删除文件
    End If
  End If
  Set fso = Nothing
End Sub
%>

<%Sub HtmlHead()%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>带进度条的ASP无组件断点续传下载----作者:午夜狂龙(Madpolice)--2005.12.25</title>
</head>
<body>
<div id="status">正在下载&nbsp;<span style="color:blue"><%=RemoteFileUrl%></span>&nbsp;,请稍候...</div>
<div>&nbsp;</div>
<div id="progress">已完成:<span id="downpercent" style="color:green"></span>&nbsp;<span id="downsize" style="color:red"><%=RangeStart%></span>&nbsp;/&nbsp;<span id="totalbytes" style="color:blue"></span>&nbsp;字节(<span id="blockavgspeed"></span>K/秒)</div>
<div>&nbsp;</div>
<div id="percent" align="center" style="display:''">
  <table style="border-collapse:collapse;" border="1" bordercolor="#666666" cellpadding="0" cellspacing="0" width="<%=PercentTableWidth%>" align="center" bgcolor="#eeeeee">
    <tr height="20">
      <td>
        <table border="0" width="" cellspacing="1" bgcolor="#0033FF" id="percentdone">
          <tr>
            <td>&nbsp;<td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</div>
<%End Sub%>

<%
'--------------------------------------------------------------------
'将秒数转换为"x小时y分钟z秒"形式
'--------------------------------------------------------------------
Function S2T(ByVal s)
  Dim x,y,z,t
  If s < 1 Then
    S2T = (s * 1000) & "毫秒"
  Else
    s = Int(s)
    x = Int(s / 3600)
    t = s - 3600 * x
    y = Int(t / 60)
    z = t - 60 * y
    If x > 0 Then
      S2T = x & "小时" & y & "分" & z & "秒"
    Else
      If y > 0 Then
        S2T = y & "分" & z & "秒"
      Else
        S2T = z & "秒"
      End If
    End If
  End If
End Function
'--------------------------------------------------------------------
%>

转载于:https://www.cnblogs.com/QDuck/archive/2006/11/17/564050.html

带进度条的ASP无组件断点续传下载代码相关推荐

  1. ASP无组件上传带进度条

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <%Option Explicit%> <% '= ...

  2. C#winform 从ftp下载文件(带进度条)、获取文件(夹)的名字

    直接用地址下载,引用进度条: ​/// <summary>/// 从ftp服务器下载文件的功能----带进度条/// </summary>/// <param name= ...

  3. Asp无组件上传进度条解决方案

    http://webuc.net/dotey/archive/2004/07/22/1334.aspx 我还是一点一点用一个实例来说明的吧,客户端HTML如下.要浏览上传附件,我们通过<inpu ...

  4. php - 基于 webuploader 视频大文件分片分段上传,支持断点续传(刷新、关闭页面、重新上传、网络中断等情况)带进度条,前端后端都有示例源码详细教程

    效果图 文件上传前先检测该文件是否已上传,如果已上传提示 "文件已存在",如果未上传则直接上传. 基于 php+webuploader的大文件分片上传,带进度条,支持断点续传(刷新 ...

  5. 艾恩ASP无组件上传最新更新说明

    利用周末的时间,对上传组件进行了比较大的更新,优化了上传代码,尽可能的降低上传的内存消耗同时提高上传的速度.具体修改及增加的功能如下: 1.文件上传类改用jscript 2.文件上传类增加几个属性   ...

  6. web app升级—带进度条的App自动更新

    带进度条的App自动更新,效果如下图所示:   技术:vue.vant-ui.5+ 封装独立组件AppProgress.vue: <template><div><van- ...

  7. atitit. 文件上传带进度条 atiUP 设计 java c# php

    atitit. 文件上传带进度条atiUP设计java c# php 1. 设计要求 1 2. 原理and架构 1 3. ui 2 4. spring mvc 2 5. springMVC.xml 3 ...

  8. 文件上传 带进度条(多种风格)

    文件上传 带进度条 多种风格 非常漂亮! 友好的提示 以及上传验证! 部分代码: <formid="form1"runat="server">< ...

  9. ajax实现上传文件的进度,基于Ajax技术实现文件上传带进度条

    1.概述 在实际的Web应该开发或网站开发过程中,经常需要实现文件上传的功能.在文件上传过程中,经常需要用户进行长时间的等待,为了让用户及时了解上传进度,可以在上传文件的同时,显示文件的上传进度条.运 ...

最新文章

  1. poj2724(二分图匹配)
  2. 如何使用R来连接各个数据库
  3. [转载]签名、加密、证书的基本原理和理解
  4. tomcat占用cpu比较多
  5. Leetcode 255. Verify Preorder Sequence in Binary Search Tree
  6. 数据库怎么看是什么编码_离婚了怎么发朋友圈?看你喜欢什么类型
  7. 熵、交叉熵、相对熵(KL 散度)意义及其关系
  8. VS对.ini文件的操作
  9. 标注工具——Electron、HTML、CSSjs的学习笔记目录
  10. java list_java中的list集合
  11. 315/433MHZ无线遥控接收解码源程序 Keil源程序 含AD格式电路图
  12. 【转ITAA上justdoit的一篇帖子】 验证OSPF中对外部路由路由的选择规则【留档】
  13. 训练创新思维的方法:曼陀罗思考法
  14. Zion无代码,流量主介绍和使用
  15. 悼念512汶川大地震遇难同胞——重建希望小学
  16. 《构建之法》第十二章 用户体验
  17. 关于EXCEL下载后无法打开的问题
  18. Python使用K-means聚类分析
  19. Pyautogui 入门
  20. 10个优秀设计网站盘点

热门文章

  1. 手动部署OpenStack环境(二:CentOS6.6虚拟机的安装及配置)
  2. mysql中日期判断的函数_MySql判断汉字、日期、数字的函数
  3. 初识软件体系结构(1-4课时)
  4. 【Postman】6 Postman 发送post请求-Json格式
  5. 中文详解phpmailer所有对象和属性
  6. Zabbix(六):项目实战之--自动发现nginx调度器及后端web服务集群、自定义参数监控...
  7. 无准备,不编程——计算机达人成长之路(15)连载
  8. 【基础复习】二:预处理、const与sizeof
  9. Linux下搭建高效的SVN
  10. Navicat Premium使用教程【比较详细】