您可以使用2个ajax调用来实现此目的,一个调用运行该进程,第二个调用定期轮询进度:
在服务器端:
public class ProgressInfo{ public int Percent {get;set;} public bool Done {get;set;}}public JsonResult DoCalculation(string id){ ProgressInfo progress = new ProgressInfo(); if(!string.IsNullOrEmpty(id)) { Session[id] = progress; } //periodicly update progress progress.Percent++;}public JsonResult GetProgress(string id){ ProgressInfo progress; if(string.IsNullOrEmpty(id) || (progress = Session[id] as ProgressInfo) == null) { return Json(new { success = false }); } if(progress.done) { Session.Remove(id); } return Json(new { success = true, done = progress.done, percent = progress.Percent });}
在客户端:
var progressID = Math.random();function doCalculation() { $.post('<%=Url.Action("DoCalcluation")%>/' + progressID); setTimeout(pollProgress, 1000);}function pollProgress() { $.post('<%=Url.Action("GetProgress")%>/' + progressID, function(response){ if(!response.success) { alert('Cannot find progress'); return; } if(response.done) { alert('Done!'); } else { alert('Progress at ' + response.precent + '%'); setTimeout(pollProgress, 1000 ); } }, 'json');}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)