ajax分批更新,动态显示进度

    技术2022-05-11  10

    文章来源:http://www.w3dev.cn/article/20090710/438.aspx

     

    示例代码点击这里下载

     

      使用Ajax分配更新数据库信息,动态显示更新进度。   适用于大批量更新,占用时间较多的更新操作。使用ajax划分为小的单元,不需要一下占用很多系统资源。   需要注意的是必须有一个自动增长的字段,值可以不连续。   示例是将一个字段的内容,包含HTML代码的,先去掉html代码,然后更新到该条记录的另一个字段中。当然你可以把动态页面的代码修改为生成静态页面或者什么之类的操作。   运行结果如下所示~

     

     

    下面为部分代码,完整代码请点击上面的连接下载

     

    update.ashx

    <%@ WebHandler Language="C#" Class="update" %> using System; using System.Web; using System.Data; using System.Data.OleDb; using System.Text.RegularExpressions; public class update : IHttpHandler { private bool IsInt(string v) { if (v == null) return false; v = v.Trim(); return Regex.IsMatch(v, @"^/d+$"); } private string RemoveHTML(string v) { if (v == null) return v; return Regex.Replace(v, " | |&[a-z]+;|'|/"|,|,|//.|。|、|//-|:|:|<[^>]+>|//s", "", RegexOptions.Compiled | RegexOptions.IgnoreCase); } public void ProcessRequest(HttpContext context) { context.Response.Charset = "utf-8"; context.Response.ContentType = "text/html"; HttpRequest Request = context.Request; string rowIndex = Request.Form["ri"], mr = Request.Form["mr"], Sql = ""; int iRowIndex = 0, iMR = 10;//如果未传递每次生成多少行,则默认10行,如果处理的行号不为整数,默认从第0行开始 if (IsInt(mr)) iMR = int.Parse(mr); if (IsInt(rowIndex)) iRowIndex = int.Parse(rowIndex); Sql = "select top " + iMR + " * from(select top " + (iMR * iRowIndex + iMR) + " [id],content from info order by [id])tmp order by [id] desc";//注意这里取数据的方式 //==========注意修改驱动 OleDbConnection cn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + context.Server.MapPath("~/data.mdb")); cn.Open(); bool Ok = true; try { DataTable dt = new DataTable(); new OleDbDataAdapter(Sql, cn).Fill(dt); OleDbCommand cm = new OleDbCommand(); cm.Connection = cn; string tmp=""; //更新数据的操作,可以修改为你需要的操作,如生成html页面什么的 foreach (DataRow dr in dt.Rows) { tmp = RemoveHTML(dr[1].ToString()); if (tmp.Length > 255) tmp = tmp.Substring(0, 255); cm.CommandText = "update info set purect='" + tmp + "' where [id]=" + dr[0]; cm.ExecuteNonQuery(); } dt.Dispose(); } catch { Ok = false; } cn.Close(); if (!Ok) throw new Exception("操作失败~~~");//抛出错误,让ajax重新执行当前操作 } public bool IsReusable { get { return false; } } }

     

    ouput.html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>ajax动态批量转换动态</title> <mce:style type="text/css"><!-- body{font-size:14px;} #proMain{border:solid 1px black;width:400px;height:20px;} #proMain div{height:100%;width:0%;background:red;color:white;overflow:hidden;line-height:20px;font-size:12px;text-align:center;} span.r{color:Red;} --></mce:style><style type="text/css" mce_bogus="1"> body{font-size:14px;} #proMain{border:solid 1px black;width:400px;height:20px;} #proMain div{height:100%;width:0%;background:red;color:white;overflow:hidden;line-height:20px;font-size:12px;text-align:center;} span.r{color:Red;}</style> <mce:script type="text/javascript" src="ajax.js" mce_src="ajax.js"></mce:script> <!--导入ajax类库--> </head> <body> <h2>注意生成过程中不要刷新当前页面,否则会导致重新生成~~~~~</h2> <input type="button" οnclick="this.disabled=true;exec();" value="开始生成" id="btnStart"/><br ><br > <div id="proMain"><div id="dvPro"></div></div><br > <div id="dvMsg"></div> <mce:script type="text/javascript"><!-- var total=parseInt('36')//总行数=============使用动态脚本设置 ,nowIndex=0//用来存储第几批数据 ,maxRows=10//每次处理的最大行数 ,sleep=5;//定义生成间隔时间,单位为秒 function UpdatePro(sql){//更新进度条 var dv=Showbo.$('dvMsg'),pro=Showbo.$('dvPro'),info=document.createElement('div'); info.innerHTML='生成<span class="r">第'+(nowIndex*maxRows+1)+'行--第'+(nowIndex*maxRows+maxRows)+'行</span>完毕,等待'+sleep+'秒后重新启动生成程序...'+sql; dv.appendChild(info); var p=Math.floor(Showbo.round((nowIndex*maxRows+maxRows)/total,2)*100); if(p>100)p=100; pro.style.width=p+'%';//设置进度条 pro.innerHTML='完成'+p+'%'; } function success(xhr){//成功回调函数 UpdatePro(xhr.responseText); if(nowIndex*maxRows+maxRows<total){//还有数据,继续启动ajax nowIndex++;//下一批 setTimeout("exec()",sleep*1000); } else{//生成完毕 var h3=document.createElement('h3');h3.innerHTML='生成完毕!'; Showbo.$('dvMsg').appendChild(h3); Showbo.$('btnStart').disabled=false; } } function Error(xhr){//错误回调函数 var dv=Showbo.$('dvMsg'),pro=Showbo.$('dvPro'),info=document.createElement('div'); info.innerHTML='生成<span class="r">第'+(nowIndex*maxRows)+'行--第'+(nowIndex*maxRows+maxRows)+'行</span>时<b>发生错误</b>,等待'+sleep+'秒后重新启动生成程序...'; dv.appendChild(info); setTimeout("exec(true)",sleep*1000); } function exec(reTry){ var dv=Showbo.$('dvMsg'),info=document.createElement('div'); info.innerHTML='<br/>'+(reTry?"正在重新":"正在")+'生成<span class="r">第'+(nowIndex*maxRows+1)+'行--第'+(nowIndex*maxRows+maxRows)+'行</span>,请等待......'; dv.appendChild(info); Showbo.Ajax.send({url:'update.ashx',method:'post',params:'mr='+maxRows+'&ri='+nowIndex,success:success,failure:Error}); } window.οnlοad=function(){ Showbo.$('dvMsg').innerHTML='总行数为:<span class="r">'+total+'</span>,' +'每次处理<span class="r">'+maxRows+'行</span>,共分为<span class="r">' +Math.ceil(total/maxRows)+'批</span>次分别更新,预计使用时间为:<span class="r">' +Math.ceil((Showbo.round(total/maxRows,2)+2)*sleep)+'s</span>'; } alert(Showbo.round(20/36,2)) // --></mce:script> </body> </html>


    最新回复(0)