上傳 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(上傳檔案的前台提交,我們讓進度條在這個裡面顯示)
<html>
<body>
<script language="javascript">
<!--
theUniqueID = (new Date()).getTime() % 1000000000;
function s() //讓資料提交的同時執行顯示進度條的函數
{
bar(); //開始執行反映上傳情況的函數
document.myform.action = "progressupload.asp?ID=" + theUniqueID; //處理上傳資料的程式
document.myform.target="up" //將提交的資料放在一個名字是up隱藏的iframe裡面處理,這樣提交的頁面就不會跳轉到處理資料的頁
document.myform.submit(); //提交表單
}
function bar()
{
bar1.style.display=''; //讓顯示上傳進度顯示的層的可見
var timeoutid=null; //這個變數是作定時器的ID
var oXMLDoc = new ActiveXObject('MSXML'); //建立'MSXML'對象
sURL = "progressbar.asp?ID=" + theUniqueID + "&temp="+Math.random(); //擷取上傳狀態資料的地址
oXMLDoc.url = sURL; //load資料
var oRoot=oXMLDoc.root; //擷取返回xml資料的根節點
if(oRoot.children != null)
{
if (oRoot.children.item(0).text-100==0) //檔案上傳結束就取消定時器
clearTimeout(timeoutid)
PercentDone.style.width=oRoot.children.item(0).text+"%"; //設定進度條的百分比例
//根據返回的資料在用戶端顯示
min.innerHTML=oRoot.children.item(1).text; //顯示剩餘時間(分鐘)
secs.innerHTML=oRoot.children.item(2).text; //顯示剩餘時間(秒鐘)
BytesDone.innerHTML=oRoot.children.item(3).text; //已上傳資料大小
BytesTotal.innerHTML=oRoot.children.item(4).text; //總大小
BytesPerSecond.innerHTML=oRoot.children.item(5).text; //傳輸速率
Information.innerHTML=oRoot.children.item(6).text; //上傳資訊
}
if (oRoot.children.item(0).text-100<0) //只要檔案沒有傳完,就每隔多少時間擷取一次資料
timeoutid = setTimeout("bar()",50) //這裡設定時間間隔是0.05秒,你也可以根據你的情況修改擷取資料時間間隔
}
//-->
</script>
<form name="myform" method="post" action="progressupload.asp" enctype="multipart/form-data" target=up>
<input type="file" name="filefield1"><br>
<input type="button" name="dosubmit" value="Upload" onclick="s()"><br>
<div id=bar1 style="display:none">
<table border="0" width="100%">
<tr>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>傳送:</b></font></td>
</tr>
<tr bgcolor="#999999">
<td>
<table border="0" width="" cellspacing="1" bgcolor="#0033FF" id=PercentDone>
<tr>
<td><font size=1> </font></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table border="0" width="100%">
<tr>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">剩餘時間:</font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="1">
<span id=min></span> 分
<span id=secs></span> 秒
(<span id=BytesDone></span>