.NET Core MVC 实现长时间任务的进度显示
.NET Core MVC 页面中有个任务按钮可能需要长时间运行,后台需要批量处理一些数据,现在希望能 显示处理进度
废话不多说,直接上代码:
主要代码 后台控制器,关键字 Flush
:
public async Task test() { var response = Response; response.Headers.Add("Content-Type", "text/event-stream"); for (var i = 0; i < 5; ++i) { await response.WriteAsync($"data: Controller {i} at {DateTime.Now}\r\r"); response.Body.Flush(); await Task.Delay(1500); } }
GarsonZhang www.yesdotnet.com
前端主要代码 关键词 EventSource
:
var source = new EventSource('url'); source.onmessage = function (event) { console.log('message:'+event.data) }; source.onopen = function (event) { console.log('open') }; source.onerror = function (event) { console.log('error') source.close(); }
GarsonZhang www.yesdotnet.com
实例中的源代码
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post 张国生