码迷,mamicode.com
首页 > Web开发 > 详细

Web页面实现后台数据处理进度与剩余时间的显示

时间:2016-06-01 18:12:25      阅读:1671      评论:0      收藏:0      [点我收藏+]

标签:

    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、前端每隔200毫秒获取进度:

<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);
                    }
                }
            });
        }, 200);
    }
    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();
    }
}

    效果图(文字错了,不是“导入进度”,而是“数据处理进度:”):

技术分享

 

Web页面实现后台数据处理进度与剩余时间的显示

标签:

原文地址:http://www.cnblogs.com/s0611163/p/5550391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!