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(上传文件的前台提交,我们让进度条在这个里面显示)
1<html>
2<body>
3<script language="javascript">
4
5<!--
6
7theUniqueID = (new Date()).getTime() % 1000000000;
8
9function s() //让数据提交的同时执行显示进度条的函数
10
11{
12
13bar(); //开始执行反映上传情况的函数
14
15document.myform.action = "progressupload.asp?ID=" + theUniqueID; //处理上传数据的程序
16
17document.myform.target="up" //将提交的数据放在一个名字是up隐藏的iframe里面处理,这样提交的页面就不会跳转到处理数据的页
18
19document.myform.submit(); //提交表单
20
21
22
23
24
25}
26
27function bar()
28
29{
30
31bar1.style.display=''; //让显示上传进度显示的层的可见
32
33var timeoutid=null; //这个变量是作定时器的ID
34
35var oXMLDoc = new ActiveXObject('MSXML'); //创建'MSXML'对象
36
37sURL = "progressbar.asp?ID=" + theUniqueID + "&temp="+Math.random(); //获取上传状态数据的地址
38
39oXMLDoc.url = sURL; //load数据
40
41var oRoot=oXMLDoc.root; //获取返回xml数据的根节点
42
43if(oRoot.children != null)
44
45{
46
47if (oRoot.children.item(0).text-100==0) //文件上传结束就取消定时器
48
49clearTimeout(timeoutid)
50
51PercentDone.style.width=oRoot.children.item(0).text+"%"; //设置进度条的百分比例
52
53//根据返回的数据在客户端显示
54
55min.innerHTML=oRoot.children.item(1).text; //显示剩余时间(分钟)
56
57secs.innerHTML=oRoot.children.item(2).text; //显示剩余时间(秒钟)
58
59BytesDone.innerHTML=oRoot.children.item(3).text; //已上传数据大小
60
61BytesTotal.innerHTML=oRoot.children.item(4).text; //总大小
62
63BytesPerSecond.innerHTML=oRoot.children.item(5).text; //传输速率
64
65Information.innerHTML=oRoot.children.item(6).text; //上传信息
66
67}
68
69if (oRoot.children.item(0).text-100<0) //只要文件没有传完,就每隔多少时间获取一次数据
70
71timeoutid = setTimeout("bar()",50) //这里设定时间间隔是0.05秒,你也可以根据你的情况修改获取数据时间间隔
72
73}
74
75//-->
76
77</script>
78<form action="progressupload.asp" enctype="multipart/form-data" method="post" name="myform" target="up">
79<input name="filefield1" type="file"/><br/>
80<input name="dosubmit" onclick="s()" type="button" value="Upload"/><br/>
81<div id="bar1" style="display:none">
82<table border="0" width="100%">
83<tr>
84<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>传送:</b></font></td>
85</tr>
86<tr bgcolor="#999999">
87<td>
88<table bgcolor="#0033FF" border="0" cellspacing="1" id="PercentDone" width="">
89<tr>
90<td><font size="1"> </font></td>
91</tr>
92</table>
93</td>
94</tr>
95<tr>
96<td>
97<table border="0" width="100%">
98<tr>
99<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">剩余时间:</font></td>
100<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">
101<span id="min"></span> 分
102
103<span id="secs"></span> 秒
104
105(<span id="BytesDone"></span> KB of
106
107<span id="BytesTotal"></span> KB 已上传)</font></td>
108</tr>
109<tr>
110<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">
111
112传送速度:</font></td>
113<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">
114<span id="BytesPerSecond"></span> KB/秒</font></td>
115</tr>
116<tr>
117<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">信息:</font></td>
118<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><span id="Information"></span></font></td>
119</tr>
120</table>
121</td>
122</tr>
123<tr></tr>
124</table>
125</div>
126<iframe name="up" style="display:none"></iframe>
127</form>
128</body>
129</html>
progressbar.asp(返回上传状况数据的文件)
1@EnableSessionState=False
1
2
3On Error Resume Next
4
5Set theProgress = Server.CreateObject("ABCUpload4.XProgress") '创建上传组件对象
6
7theProgress.ID = Request.QueryString("ID")
8
9'将返回数据以xml格式输出
10
1<plan>
2<percentdone>```
3=theProgress.PercentDone
4```</percentdone>
5<min>```
6=Int(theProgress.SecondsLeft/60)
7```</min>
8<secs>```
9=theProgress.SecondsLeft Mod 60
10```</secs>
11<bytesdone>```
12=Round(theProgress.BytesDone / 1024, 1)
13```</bytesdone>
14<bytestotal>```
15=Round(theProgress.BytesTotal / 1024, 1)
16```</bytestotal>
17<bytespersecond>```
18=Round(theProgress.BytesPerSecond/1024, 1)
19```</bytespersecond>
20<information>```
21=theProgress.Note
22```</information>
23</plan>
progressupload.asp(处理上传文件)
1@EnableSessionState=False
1
2
3Response.Expires = -10000
4
5Server.ScriptTimeOut = 300
6
7
8
9Set theForm = Server.CreateObject("ABCUpload4.XForm")
10
11theForm.Overwrite = True
12
13theForm.MaxUploadSize = 8000000
14
15theForm.ID = Request.QueryString("ID")
16
17Set theField = theForm("filefield1")(1)
18
19If theField.FileExists Then
20
21theField.Save theField.FileName
22
23End If
24
1<html>
2<body>
3
4传送结束
5
6</body>
7</html>
对于其他组件上传,原理也就差不多了,只是方法不一样罢了。
希望大家有什么意见和建议和我联系:)