asp中使用组件上传时,有点麻烦就是不知道上传进度是多少,虽然有的提供了上传进度条,例如abcupload(下载地址:http://www.websupergoo.com)已经提供了显示当前上传状态的方法,里面就有专门的实例(在安装目录的examples下面的progressupload.htm),它是提交数据的同时弹出一个反映进度的页面(progressbar.asp),然后通过这个页面自刷新来每隔一点时间获取当时的上传状态,然后把它们显示出来,但是用页面自刷新的方法效率比较低,不不好调节刷新时间间隔(最小间隔为1秒),而且服务器端返回数据量大,所以不能很好的实时反映上传情况。在客户端使用javascript调用msxml对象和settimout方法去定时load一个xml文件可以实现无刷新定时获取服务器端数据,在这里可以让progressbar.asp输出xml格式的数据,然后供客户端load,仅返回必要的几个参数,这样:页面不刷新;传输的数据少,不需要把所有数据全传到客户端 ,只传输反映状态的数据,如果定时器设置的时间足够小,那么我们就可以”实时”看到上传的状况了。以下就以abcupload4为例来说明怎么来制作实时的文件上传进度条。(注:我们在abcupload自带例子基础上改进。)
progressupload.htm
(上传文件的前台提交,我们让进度条在这个里面显示)
</p><p></p><p><html></p><p><body></p><p></p><p>
<script language=”javascript”></p><p><!–</p>
<p>theuniqueid = (new date()).gettime() % 1000000000;</p><p>function s() file://让数据提交的同时执行显示进度条的函数</p><p>{</p><p>
bar(); file://开始执行反映上传情况的函数</p><p>
document.myform.action = “progressupload.asp?id=” + theuniqueid;
file://处理上传数据的程序</p><p>
document.myform.target=”up” file://将提交的数据放在一个名字是up隐藏的iframe里面处理,这样提交的页面就不会跳转到处理数据的页
document.myform.submit(); file://提交表单</p><p></p><p></p><p>}
</p><p>function bar()</p><p>{</p><p>
bar1.style.display= ; file://让显示上传进度显示的层的可见</p><p>
var timeoutid=null; file://这个变量是作定时器的id</p><p>
var oxmldoc = new activexobject( msxml );
file://创建 msxml 对象</p><p>
surl = “progressbar.asp?id=” + theuniqueid + “&temp=”+math.random();
file://获取上传状态数据的地址</p><p>
oxmldoc.url = surl;
file://load数据
var oroot=oxmldoc.root;
file://获取返回xml数据的根节点
if(oroot.children != null) {
if (oroot.children.item(0).text-100==0) file://文件上传结束就取消定时器
cleartimeout(timeoutid)
percentdone.style.width=oroot.children.item(0).text+”%”; file://设置进度条的百分比例
file://根据返回的数据在客户端显示
min.innerhtml=oroot.children.item(1).text; file://显示剩余时间(分钟)
secs.innerhtml=oroot.children.item(2).text; file://显示剩余时间(秒钟)
bytesdone.innerhtml=oroot.children.item(3).text; file://已上传数据大小
bytestotal.innerhtml=oroot.children.item(4).text; file://总大小
bytespersecond.innerhtml=oroot.children.item(5).text; file://传输速率
information.innerhtml=oroot.children.item(6).text; file://上传信息
}
if (oroot.children.item(0).text-100<0) file://只要文件没有传完,就每隔多少时间获取一次数据
timeoutid = settimeout(“bar()”,50)
file://这里设定时间间隔是0.05秒,你也可以根据你的情况修改获取数据时间间隔
}</p><p>//–></p><p></script>nbsp; </tr></p><p>
</table></p><p> </td></p><p> </tr></p><p> <tr></p><p><td></p><p>
<table border=”0″ width=”100%”></p><p><tr></p><p> <td><font face=”verdana, arial, helvetica, sans-serif” size=”1″>
剩余时间:</font></td></p><p>
<td><font face=”verdana, arial, helvetica, sans-serif” size=”1″></p><p>
<span id=min></span>分</p><p><span id=secs></span> 秒 </p><p>
(<span id=bytesdone></span> kb of </p><p><span id=bytestotal></span> kb 已上传)</font></td></p><p>
</tr></p><p> <tr></p><p>
<td><font face=”verdana, arial, helvetica, sans-serif” size=”1″></p><p>
传送速度:</font></td></p><p>
<td><font face=”verdana, arial, helvetica, sans-serif” size=”1″></p><p>
<span id=bytespersecond></span> kb/秒</font></td></p><p>
</tr></p><p> <tr></p><p>
<td><font face=”verdana, arial, helvetica, sans-serif” size=”1″>信息:</font></td></p><p>
<td><font face=”verdana, arial, helvetica, sans-serif” size=”1″><span id=information>
</span></font></td></p><p> </tr></p><p> </table></p><p> </td></p><p> </tr></p><p> <tr></tr></p><p></table></p><p></div></p><p><iframe name=”up” style=”display:none”></iframe></p><p></form></p><p></p><p></body></p><p>
</html></p><p></p><p>progressbar.asp(返回上传状况数据的文件)
</p><p></p><p><%@enablesessionstate=false%></p><p><%</p><p>
on error resume next</p><p>set theprogress = server.createobject(“abcupload4.xprogress”) 创建上传组件对象</p><p>theprogress.id = request.querystring(“id”)</p><p> 将返回数据以xml格式输出
</p><p>%></p><p><?xml version=”1.0″ encoding=”gb2312″ ?></p><p><plan></p><p> <percentdone><%=theprogress.percentdone%></percentdone></p><p>
<min><%=int(theprogress.secondsleft/60)%></min></p><p>
<secs><%=theprogress.secondsleft mod 60%></secs></p><p>
<bytesdone><%=round(theprogress.bytesdone / 1024, 1)%></bytesdone></p><p>
; <bytestotal><%=round(theprogress.bytestotal / 1024, 1)%></bytestotal></p><p> <bytespersecond><%=round(theprogress.bytespersecond/1024, 1)%></bytespersecond></p><p> <information><%=theprogress.note%></information></p><p></plan></p><p></p>
<p>progressupload.asp(处理上传文件)</p><p></p><p><%@enablesessionstate=false%>
</p><p><%</p><p>response.expires = -10000</p><p>server.scripttimeout = 300</p>
<p></p><p>set theform = server.createobject(“abcupload4.xform”)</p>
<p>theform.overwrite = true</p><p>theform.maxuploadsize = 8000000</p>
<p>theform.id = request.querystring(“id”)</p><p>set thefield = theform(“filefield1”)(1)
</p><p>if thefield.fileexists then</p><p> thefield.save thefield.filename</p><p>end if</p><p>%></p><p></p><p><html></p><p><body></p><p>传送结束</p><p></body></p><p>
</html></p><p></p><p></p><p>
对于其他组件上传,原理也就差不多了,只是方法不一样罢了
希望大家有什么意见和建议和我联系:)
junminliu@msn.com
</p><p></p><p>
<form name=”myform” method=”post” action=”progressupload.asp” enctype=”multipart/form-data” target=up>
</p><p><input type=”file” name=”filefield1″><br></p><p><input type=”button” name=”dosubmit” value=”upload” onclick=”s()”><br></p><p><div id=bar1 style=”display:none”>
</p><p><table border=”0″ width=”100%”></p><p> <tr></p><p>
<td><font face=”verdana, arial, helvetica, sans-serif” size=”2″><b>传送:</b></font></td></p><p> </tr></p><p>
<tr bgcolor=”#999999″></p><p> <td></p><p>
<table border=”0″ width=”” cellspacing=”1″ bgcolor=”#0033ff” id=percentdone></p><p>
<tr></p><p><td><font size=1> </font></td></p><p>&
