asp.net線程批量導(dǎo)入數(shù)據(jù)時(shí)通過(guò)ajax獲取執(zhí)行狀態(tài)
前言
最近因?yàn)楣ぷ髦杏龅揭粋€(gè)需求,需要做了一個(gè)批量導(dǎo)入功能,但長(zhǎng)時(shí)間運(yùn)行沒(méi)個(gè)反饋狀態(tài),很容易讓人看了心急,產(chǎn)生各種臆想!為了解決心里障礙,寫(xiě)了這么個(gè)功能。
通過(guò)線程執(zhí)行導(dǎo)入,并把正在執(zhí)行的狀態(tài)存入session,既共享執(zhí)行狀態(tài),通過(guò)ajax調(diào)用session里的執(zhí)行狀態(tài),從而實(shí)現(xiàn)反饋導(dǎo)入狀態(tài)的功能!
上代碼: 前端頁(yè)面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>批量導(dǎo)入數(shù)據(jù)</title> <style type="text/css"> .pop_body_con { width: 310px; position: fixed; top: 50%; left: 50%; margin-left: -150px; background: #eee; display:none; } .pop_body_con .pop_head { width: auto; padding: 10px 0; background: #fff; } .pop_body_con .pop_head a { display: block; color: #717274; font-size: 12px; text-decoration: none; text-align: center; } .pop_box { width: auto; overflow: hidden; padding: 45px 10px; } .pop_box .pop_text { float: left; } .pop_box .pop_text p { padding: 0; margin: 0; font-size: 12px; line-height: 18px; color: #717274;} .pop_box .progress_bar_con { float: left; width: 220px; position: relative; z-index: 2; } .pop_box .progress_bar_con p { margin: 0; padding: 0; font-size: 12px; color: #fff; line-height: 18px; width: 100%; text-align: center; position: absolute; left: 0; top: 0; z-index: 4; } .pop_box .progress_bar_con .progress_bar_start { width: 100%; height: 18px; background: #C4C0C0; } .pop_box .progress_bar_con .progress_bar_end { width: 16%; height: 18px; background: #2bd35d; position: absolute; left: 0; top: 0; z-index: 3; } .pop_box .progress_bar_con { float: left; } #loading-mask { width: 100%; height: 100%; position: fixed; top: 0px; left: 0px; z-index: 0; background-color: rgba(0, 0, 0, 0.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" }, success: function (msg) { if (msg != "null") { msg = eval('(' + msg + ')'); if (msg.being == 100) { setProcessBar(1, 1); $(".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 style="background-color: #fff;"> <input id="startImport" type="button" value="導(dǎo)入數(shù)據(jù)" /> <div id="loading-mask" ></div> <div class="pop_body_con"> <div class="pop_head"> <a href="javascript:;">正在導(dǎo)入…請(qǐng)勿操作!</a> </div> <div class="pop_box"> <div class="pop_text"> <p>導(dǎo)入進(jìn)度:</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>
后臺(tái)頁(yè)面:
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,方便共享執(zhí)行狀態(tài) 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; //返回狀態(tài)值 stateHash["being"] = beingV; //正在執(zhí)行值 stateHash["count"] = CountV; //總值 stateHash["action"] = action; //總值 return stateHash; } }
ok,共享完畢!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
- C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中
- Asp.Net使用Bulk實(shí)現(xiàn)批量插入數(shù)據(jù)
- C#/.Net 中快速批量給SQLite數(shù)據(jù)庫(kù)插入測(cè)試數(shù)據(jù)
- asp.net新聞列表生成靜態(tài)頁(yè)之批量和單頁(yè)生成
- 在ASP.NET 2.0中操作數(shù)據(jù)之六十二:GridView批量更新數(shù)據(jù)
- 在ASP.NET 2.0中操作數(shù)據(jù)之六十四:GridView批量添加數(shù)據(jù)
- 在ASP.NET 2.0中操作數(shù)據(jù)之三十七:DataList批量更新
- ajax readyState的五種狀態(tài)詳解
- AJAX(XMLHttpRequest.status)狀態(tài)碼
- javascript學(xué)習(xí)筆記(七)Ajax和Http狀態(tài)碼
相關(guān)文章
利用MS AJAX注冊(cè)Javascript命名空間并創(chuàng)建類
利用MS AJAX注冊(cè)Javascript命名空間并創(chuàng)建類...2007-10-10GridView自定義分頁(yè)實(shí)例詳解(附demo源碼下載)
這篇文章主要介紹了GridView自定義分頁(yè)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了GridView自定義分頁(yè)所涉及的樣式布局及功能實(shí)現(xiàn)相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-03-03VC用Ado接口連接和使用數(shù)據(jù)庫(kù)及注意事項(xiàng)
進(jìn)行方法調(diào)用時(shí),所傳遞的參數(shù)的類型的轉(zhuǎn)換(可能存在比本文更簡(jiǎn)便的處理方法但我未發(fā)現(xiàn)) ,需要了解的朋友可以參考下2012-12-12ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探
這篇文章主要介紹了ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探,幫助大家更好的理解和學(xué)習(xí)使用.NET技術(shù),感興趣的朋友可以了解下2021-03-03微信公眾平臺(tái)開(kāi)發(fā)之獲得ACCESSTOKEN .Net代碼解析
這篇文章主要為大家詳細(xì)解析了微信公眾平臺(tái)開(kāi)發(fā)之獲得ACCESSTOKEN .Net代碼,感興趣的小伙伴們可以參考一下2016-06-06asp.net驗(yàn)證一個(gè)字符串是否符合指定的正則表達(dá)式
asp.net檢驗(yàn)字符串是否滿足指定正則表達(dá)式2008-05-05