温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • asp.net线程批量导入数据时通过ajax获取执行状态的代码分享

asp.net线程批量导入数据时通过ajax获取执行状态的代码分享

发布时间:2021-09-09 17:16:26 阅读:115 作者:chen 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍“asp.net线程批量导入数据时通过ajax获取执行状态的代码分享”,在日常操作中,相信很多人在asp.net线程批量导入数据时通过ajax获取执行状态的代码分享问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”asp.net线程批量导入数据时通过ajax获取执行状态的代码分享”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

上代码: 前端页面

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>批量导入数据</title>
 <style type="text/css">
  .pop_body_con { width310pxposition: fixed; top50%left50%margin-left: -150pxbackground#eeedisplay:none; }
   .pop_body_con .pop_head { width: auto; padding10px 0background#fff; }
    .pop_body_con .pop_head a { display: block; color#717274font-size12pxtext-decoration: none; text-align: center; }
  .pop_box { width: auto; overflow: hidden; padding45px 10px; }
   .pop_box .pop_text { float: left; }
    .pop_box .pop_text p { padding0margin0font-size12pxline-height18pxcolor#717274;}
   .pop_box .progress_bar_con { float: left; width220pxposition: relative; z-index2; }
    .pop_box .progress_bar_con p { margin0padding0font-size12pxcolor#fffline-height18pxwidth100%; 
            text-align: center; position: absolute; left0top0z-index4; }
    .pop_box .progress_bar_con .progress_bar_start { width100%height18pxbackground#C4C0C0; }
    .pop_box .progress_bar_con .progress_bar_end { width16%height18pxbackground#2bd35dposition: absolute; left0top0z-index3; }
   .pop_box .progress_bar_con { float: left; }
  #loading-mask { width100%height100%position: fixed; top0pxleft0pxz-index0background-colorrgba(0000.34902); display: none; }
 </style>
 <script src="ajax-master/jquery.js"></script>
 <script>
  var MyInterval;

  $(function () {
   $("#startImport").click(function () {
    MyInterval = setInterval(getState, 1000);
   });
  });
  
  function getState() {
   $.ajax({
    url"test.aspx",
    type"Post",
    data: { action"getSession" },
    successfunction (msg) {
     if (msg != "null") {
      msg = eval('(' + msg + ')');
      if (msg.being == 100) {
       setProcessBar(11);
       $(".pop_body_con").hide();
       $("#loading-mask").hide();
       clearInterval(MyInterval);
      }
      else {
       $(".pop_body_con").show();
       $("#loading-mask").show();
       setProcessBar(msg.being, msg.count)
      }
     }
    }
   });
  }

  function setProcessBar(exeFlag, exeMax) {
   $("#progressbar_text").html(parseInt(roundFun(exeFlag / exeMax, 2) * 100) + "%");
   $("#progressbar_bar").attr("style""width:" + parseInt(roundFun(exeFlag / exeMax, 2) * 100) + "%;");
  }

  function roundFun(number, X) {
   X = (!X ? 2 : X);
   return Math.round(number * Math.pow(10, X)) / Math.pow(10, X);
  }
 </script>
</head>
<body >
 <input id="startImport" type="button" value="导入数据" />
 <div id="loading-mask" ></div>
 <div class="pop_body_con">
  <div class="pop_head">
   <a href="javascript:;">正在导入…请勿操作!</a>
  </div>
  <div class="pop_box">
   <div class="pop_text">
    <p>导入进度:</p>
   </div>
   <div class="progress_bar_con">
    <p id="progressbar_text">0%</p>
    <div class="progress_bar_start"></div>
    <div class="progress_bar_end" id="progressbar_bar"></div>
   </div>
  </div>
 </div>
</body>
</html>

后台页面:

using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
  string action = Request.Form["action"];
  if (!string.IsNullOrEmpty(action))
  {
   Hashtable temp = tmethod();
   if (temp == null)
   {
    Thread trd = new Thread(new ParameterizedThreadStart(insertData));
    trd.Start(action);
   }
   else
   {
    if (temp["reCode"].ToString() == "100")
    {
     
     Session.Remove("process");
    }
   }

   JavaScriptSerializer ser = new JavaScriptSerializer();
   String jsonStr = ser.Serialize(temp);
   Response.Write(jsonStr);
   Response.End();
  }
 }


 public Hashtable tmethod()
 {
  return (Hashtable)Session["process"];
 }

 private void insertData(object obj)
 {
  string action = obj.ToString();
  int tCount = 100;
  for (int i = 0; i < tCount; i++)
  {
   Hashtable stateHash = setStateVal(0, i, tCount, action);
   Session["process"] = stateHash; //存入session,方便共享执行状态
   Thread.Sleep(500);
  }
  Session["process"] = setStateVal(100, tCount, tCount, action);
  Thread.CurrentThread.Abort();
 }

 private Hashtable setStateVal(int code, int beingV, int CountV, string action)
 {
  Hashtable stateHash = new Hashtable();
  stateHash["reCode"] = code; //返回状态值
  stateHash["being"] = beingV;  //正在执行值
  stateHash["count"] = CountV;  //总值
  stateHash["action"] = action;  //总值
  return stateHash;
 }
}

ok,共享完毕!

到此,关于“asp.net线程批量导入数据时通过ajax获取执行状态的代码分享”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×