您的位置:手机购彩平台 > 手机购彩平台-编程 > 5、后台计算进度

5、后台计算进度

2019-09-24 15:47

1、页面后台代码添加如下属性:

图片 1图片 2

/// <summary>/// 总数/// </summary>private double total{    set    {        Session["DPMS.POP.POP_ExcelLeadIn_total"] = value;    }    get    {        if (Session["DPMS.POP.POP_ExcelLeadIn_total"] == null)        {            return 0;        }        return Convert.ToDouble(Session["DPMS.POP.POP_ExcelLeadIn_total"]);    }}/// <summary>/// 当前进度/// </summary>private int cur{    set    {        Session["DPMS.POP.POP_ExcelLeadIn_cur"] = value;    }    get    {        if (Session["DPMS.POP.POP_ExcelLeadIn_cur"] == null)        {            return 0;        }        return Convert.ToInt32(Session["DPMS.POP.POP_ExcelLeadIn_cur"]);    }}/// <summary>/// 错误信息/// </summary>private string errMsg{    set    {        Session["DPMS.POP.POP_ExcelLeadIn_errMsg"] = value;    }    get    {        if (Session["DPMS.POP.POP_ExcelLeadIn_errMsg"] == null)        {            return string.Empty;        }        return Session["DPMS.POP.POP_ExcelLeadIn_errMsg"].ToString();    }}/// <summary>/// 开始时间/// </summary>private DateTime startTime{    set    {        Session["DPMS.POP.POP_ExcelLeadIn_startTime"] = value;    }    get    {        if (Session["DPMS.POP.POP_ExcelLeadIn_startTime"] == null)        {            return DateTime.Now;        }        return Convert.ToDateTime(Session["DPMS.POP.POP_ExcelLeadIn_startTime"]);    }}

View Code

2、在处理数据的开始,初始化total和startTime变量:

total = int.Parse(dataSet.Tables[0].Rows[0][0].ToString;
startTime = DateTime.Now;

3、在处理数据过程中,不断累加cur:

cur++;

4、前端每隔500毫秒获取进度:

图片 3图片 4

<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  {                    if (data == "导入进度:100.00%") {                        clearInterval;                        $("#msg").html;                        alert("导入成功");                    } else {                        if (data.indexOf == 0) {                            clearInterval;                        }                        $("#msg").html;                    }                }            });        }, 500);    }    refreshProcess();</script>

View Code

5、后台计算进度:

图片 5图片 6

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            {                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    {        Response.Write;        Response.End();    }}

View Code

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

图片 7

本文由手机购彩平台发布于手机购彩平台-编程,转载请注明出处:5、后台计算进度

关键词: