在最近的一個項目中,有一個上傳功能:上傳一個cvs文件,然後解析此文件並寫入數據庫
由於經常需要傳很大的文件,客戶完成此功能往往需要40分鐘,在這個過程中,頁面也沒有任何提示,用戶體驗非常不好?
為何不用AJax作一個進度條呢?
分兩步完成此需求:
一:寫一個簡單的AJax,實現最簡單的進度條功能。
二:把此進度條改造為項目可用的進度條。
一:最簡單的進度條
1。客戶端每2秒發送一個createXMLHttpRequest請求給服務端.並得到服務端返回的進度數據.根據服務端返回的數據,用Javascript更新一個table的width,
這樣就模擬了一個進度條.
progressBar.Html.內容如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.01 Transitional//EN">
<Html>
<head>
<title>AJax Progress Bar</title>
<script type="text/Javascript">...
var XMLHttp;
var key;
function createXMLHttpRequest() ...{
if (window.ActiveXObject) ...{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) ...{
xmlHttp = new XMLHttpRequest();
}
}
function go() ...{
createXMLHttpRequest();
clearBar();
var url = "ProgressBarServlet?task=create";
var button = document.getElementById("go");
button.disabled = true;
XMLHttp.open("GET", url, true);
XMLHttp.onreadystatechange = goCallback;
XMLHttp.send(null);
}
function goCallback() ...{
if (XMLHttp.readyState == 4) ...{
if (XMLHttp.status == 200) ...{
setTimeout("pollServer()", 2000);
}
}
}
function pollServer() ...{
createXMLHttpRequest();
var url = "ProgressBarServlet?task=poll&key=" + key;
XMLHttp.open("GET", url, true);
XMLHttp.onreadystatechange = pollCallback;
XMLHttp.send(null);
}
function pollCallback() ...{
if (XMLHttp.readyState == 4) ...{
if (XMLHttp.status == 200) ...{
var percent_complete = xmlHttp.responseXML.getElementsByTagName("percent")[0].firstChild.data;
var progress = document.getElementById("progress");
var progressPersent = document.getElementById("progressPersent");
progress.width = percent_complete + "%";
progressPersent.innerHtml = percent_complete + "%";
if (percent_complete < 100) ...{
setTimeout("pollServer()", 2000);
} else ...{
document.getElementById("complete").innerHtml = "Complete!";
//document.getElementById("go").disabled = false;
}
}
}
}
function clearBar() ...{
var progress_bar = document.getElementById("progressBar");
var progressPersent = document.getElementById("progressPersent");
var complete = document.getElementById("complete");
progress_bar.style.visibility = "visible"
progressPersent.innerHtml = " ";
complete.innerHtml = "Begin to upload this file...";
}
</script>
</head>
<body>
<div id="progressBar" style="padding:0px;border:solid black 0px;visibility:hidden">
<table width="300" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center" id="progressPersent">86%</td>
</tr>
<tr >
<td>
<table width="100%" border="1" cellspacing="0" cellpadding="0" bordercolor="#000000">
<tr>
<td>
<table width="1%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FF0000" id="progress">
<tr>
<td> </td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" id="complete">completed</td>
</tr>
</table>
</div>
<input id = "go" name="run" type="button" value="run" onClick="go();">
</body>
</Html>
2:一個模擬servlet:ProgressBarServlet1。Java,內容如下:
package com.cyberobject.lcl.AJax;
import Java.io.*;
import Javax.servlet.*;
import Javax.servlet.http.*;
/**
*
* @author nate
* @version
*/
public class ProgressBarServlet extends HttpServlet {
private int counter = 1;
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String task = request.getParameter("task");
String res = "";
if (task.equals("create")) {
res = "<key>1</key>";
counter = 1;
}
else {
String percent = "";
switch (counter) {
case 1: percent = "10"; break;
case 2: percent = "23"; break;
case 3: percent = "35"; break;
case 4: percent = "51"; break;
case 5: percent = "64"; break;
case 6: percent = "73"; break;
case 7: percent = "89"; break;
case 8: percent = "100"; break;
}
counter++;
res = "<percent>" + percent + "</percent>";
}
PrintWriter out = response.getWriter();
response.setContentType("text/XML");
response.setHeader("Cache-Control", "no-cache");
out.println("<response>");
out.println(res);
out.println("</response>");
out.close();
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
}
3:在web。XML中配置好servlet映射:
<!-- Action Servlet Mapping -->
<servlet>
<servlet-name>ProgressBarServlet</servlet-name>
<display-name>ProgressBarServlet</display-name>
<servlet-class>com.cyberobject.lcl.AJax.ProgressBarServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProgressBarServlet</servlet-name>
<url-pattern>/ProgressBarServlet</url-pattern>
</servlet-mapping>
此時進度條已經可以運行了。接下來的工作就是把它移植到我們的系統。
二:
1:在寫數據庫的class DbOperater中,增加一個progress屬性
private int progress;
2:在寫數據庫的class中,增加一個getProgress()方法:
public int getProgress()
{
return progress;
}
3: 在寫庫的for循環中, progress++;
4:在調用DbOperater的servlet中調用DbOperater的getProgress()方法,這樣就為進度條提供了實時數據.
5:另外:servlet的doGet()用來獲得進度數據,doPost()用來上傳文件和寫庫操作.彼此分工明確.
特此存檔