1、頁面後臺代碼添加如下靜態變數: 2、在處理數據的開始,初始化total和startTime變數: 3、在處理數據過程中,不斷累加cur: 4、前端每隔200毫秒獲取進度: 5、後臺計算進度: 效果圖(文字錯了,不是“導入進度”,而是“數據處理進度:”): ...
1、頁面後臺代碼添加如下靜態變數:
/// <summary> /// 總數 /// </summary> private static double total = 0; /// <summary> /// 當前進度 /// </summary> private static int cur = 0; /// <summary> /// 錯誤信息 /// </summary> private static string errMsg = string.Empty; /// <summary> /// 開始時間 /// </summary> private static DateTime startTime = DateTime.Now;
2、在處理數據的開始,初始化total和startTime變數:
total = int.Parse(dataSet.Tables[0].Rows[0][0].ToString());
startTime = DateTime.Now;
3、在處理數據過程中,不斷累加cur:
cur++;
4、前端每隔500毫秒獲取進度:
<script type="text/javascript"> //更新進度 function refreshProcess() { var itv = setInterval(function () { $.ajax({ url: "ExcelLeadIn.aspx?action=getProcess&t=" + new Date().valueOf(), type: "POST", data: {}, success: function (data) { if (data == "導入進度:100.00%") { clearInterval(itv); $("#msg").html(data); alert("導入成功"); } else { if (data.indexOf("錯誤:") == 0) { clearInterval(itv); } $("#msg").html(data); } } }); }, 500); } refreshProcess(); </script>
5、後臺計算進度:
protected void Page_Load(object sender, EventArgs e) { string result = string.Empty; if (Request["action"] == "getProcess") { try { if (string.IsNullOrEmpty(errMsg)) { if (total == 0) { result = "導入進度:0%"; } else { DateTime now = DateTime.Now; TimeSpan ts = now - startTime; string time = string.Empty; double per = cur / total; if (per > 0) { double totalSeconds = ts.TotalSeconds / per - ts.TotalSeconds; if (totalSeconds > 60) { time = (int)Math.Round(totalSeconds / 60) + "分"; } else { time = (int)Math.Round(totalSeconds) + "秒"; } } string percent = (cur / total * 100).ToString("0.00"); if (percent == "100.00") { cur = 0; total = 0; result = string.Format("導入進度:{0}%", percent); } else { result = string.Format("導入進度:{0}%,剩餘時間:{1}", percent, time); } } } else { result = "錯誤:" + errMsg; } } catch (Exception ex) { result = "錯誤:" + ex.Message; } } if (!string.IsNullOrEmpty(result)) { Response.Write(result); Response.End(); } }
效果圖(文字錯了,不是“導入進度”,而是“數據處理進度:”):
修改以支持多用戶同時導入:
1、頁面後臺代碼添加如下靜態變數:
/// <summary> /// 總數 /// </summary> private static Dictionary<string, double> total = new Dictionary<string, double>(); /// <summary> /// 當前進度 /// </summary> private static Dictionary<string, int> cur = new Dictionary<string, int>(); /// <summary> /// 錯誤信息 /// </summary> private static Dictionary<string, string> errMsg = new Dictionary<string, string>(); /// <summary> /// 開始時間 /// </summary> private static Dictionary<string, DateTime> startTime = new Dictionary<string, DateTime>(); #region 總數 private void setTotal(string userId, double value) { if (total.ContainsKey(userId)) { total[userId] = value; } else { total.Add(userId, value); } } private double getTotal(string userId) { if (total.ContainsKey(userId)) { return total[userId]; } else { return 0; } } #endregion #region 當前進度 private void addCur(string userId) { if (cur.ContainsKey(userId)) { cur[userId]++; } else { cur.Add(userId, 1); } } private void setCur(string userId, int value) { if (cur.ContainsKey(userId)) { cur[userId] = value; } else { cur.Add(userId, value); } } private int getCur(string userId) { if (cur.ContainsKey(userId)) { return cur[userId]; } else { return 0; } } #endregion #region 錯誤信息 private void setErrMsg(string userId, string value) { if (errMsg.ContainsKey(userId)) { errMsg[userId] = value; } else { errMsg.Add(userId, value); } } private string getErrMsg(string userId) { if (errMsg.ContainsKey(userId)) { return errMsg[userId]; } else { return string.Empty; } } #endregion #region 開始時間 private void setStartTime(string userId, DateTime value) { if (startTime.ContainsKey(userId)) { startTime[userId] = value; } else { startTime.Add(userId, value); } } private DateTime getStartTime(string userId) { if (startTime.ContainsKey(userId)) { return startTime[userId]; } else { return DateTime.Now; } } #endregion
2、在處理數據的開始,初始化total和startTime變數:
setTotal(loginUser.USER_ID, int.Parse(dataSet.Tables[0].Rows[0][0].ToString())); //總數 setStartTime(loginUser.USER_ID, DateTime.Now); //開始時間
3、在處理數據過程中,不斷累加cur:
addCur(loginUser.USER_ID); //進度累加
4、前端每隔500毫秒獲取進度:
<script type="text/javascript"> //更新進度 function refreshProcess() { var itv = setInterval(function () { $.ajax({ url: "ExcelLeadIn.aspx?action=getProcess&t=" + new Date().valueOf(), type: "POST", data: {}, success: function (data) { if (data == "導入進度:100.00%") { clearInterval(itv); $("#msg").html(data); alert("導入成功"); } else { if (data.indexOf("錯誤:") == 0) { clearInterval(itv); } $("#msg").html(data); } } }); }, 500); } refreshProcess(); </script>
5、後臺計算進度:
protected void Page_Load(object sender, EventArgs e) { string result = string.Empty; if (Request["action"] == "getProcess") { try { LoginEntity loginUser = (LoginEntity)this.Session[BasePage.LOGIN_USER_KEY]; string userId = loginUser.USER_ID; if (string.IsNullOrEmpty(getErrMsg(userId))) { if (getTotal(userId) == 0) { result = "導入進度:0%"; } else { DateTime now = DateTime.Now; TimeSpan ts = now - getStartTime(userId); string time = string.Empty; double per = getCur(userId) / getTotal(userId); if (per > 0) { double totalSeconds = ts.TotalSeconds / per - ts.TotalSeconds; if (totalSeconds > 60) { time = (int)Math.Round(totalSeconds / 60) + "分"; } else { time = (int)Math.Round(totalSeconds) + "秒"; } } string percent = (getCur(userId) / getTotal(userId) * 100).ToString("0.00"); if (percent == "100.00") { setCur(userId, 0); setTotal(userId, 0); result = string.Format("導入進度:{0}%", percent); } else { result = string.Format("導入進度:{0}%,剩餘時間:{1}", percent, time); } } } else { result = "錯誤:" + errMsg; } } catch (Exception ex) { result = "錯誤:" + ex.Message; } } if (!string.IsNullOrEmpty(result)) { Response.Write(result); Response.End(); } }