在ASP.Net MVC的长时间运行的服务器的进度条调用 [英] Progress bar for long running server calls in ASP.Net MVC
问题描述
我只是想创建一个进度条,而长时间运行的服务器调用。我无法创建一个AJAX POST请求控制器,而控制器是做一个长期运行的工作。
I just want to create a progress bar while long running server calls. I could not create a ajax post request to the controller while the controller is doing a long running job.
我想创建一个额外行动来得到当前的长期运行的任务的实际语句。 我试图创建民意调查在Ajax请求的话,我可以能够从服务器端返回的状态,并在客户端的进度条显示。有任何想法吗 ?
I want to create an additional action to get the actual statement of current long running task. I tried to create poll in ajax request then i can able to return the status from the server side and display it in a client side progress bar. Any ideas ?
推荐答案
正确的,最简单的就是使用SignalR方式。请下载Microsoft SignalR在 https://www.nuget.org/packages/ Microsoft.AspNet.SignalR / 2.1.2
The right and easiest way to do this is with SignalR. Please download Microsoft SignalR in https://www.nuget.org/packages/Microsoft.AspNet.SignalR/2.1.2
在创建项目的路径被称为枢纽单独的文件夹毂类,添加两个类文件到集线器夹
Create a hub class in separate folder in project path called hubs, add two class files into the hubs folder
Startup.cs
Startup.cs
using Owin;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
}
ProgressHub.cs
ProgressHub.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace RealTimeProgressBar
{
public class ProgressHub : Hub
{
public string msg = "Initializing and Preparing...";
public int count = 1;
public static void SendMessage(string msg , int count)
{
var message = "Process completed for " + msg;
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
hubContext.Clients.All.sendMessage(string.Format(message), count);
}
public void GetCountAndMessage()
{
Clients.Caller.sendMessage(string.Format(msg), count);
}
}
}
在控制器,
//组装
using Microsoft.AspNet.SignalR;
using RealTimeProgressBar;
//调用这个方法,你的工作action里面
//call this method inside your working action
ProgressHub.SendMessage("initializing and preparing", 2);
在视图中,
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
$(document).ready(function () {
$("#progressBar").kendoProgressBar({
min: 0,
max: 100,
type: "percent",
});
});
function StartInvoicing()
{
var progressNotifier = $.connection.progressHub;
// client-side sendMessage function that will be called from the server-side
progressNotifier.client.sendMessage = function (message, count) {
// update progress
UpdateProgress(message, count);
//alert(message);
};
// establish the connection to the server and start server-side operation
$.connection.hub.start().done(function () {
// call the method CallLongOperation defined in the Hub
progressNotifier.server.getCountAndMessage();
});
}
//更新进度条
// Update the progress bar
function UpdateProgress(message, count) {
var result = $("#result");
result.html(message);
$("#progressBar").data("kendoProgressBar").value(count);
}
有关更多详情,请参阅现有的一些文章与谷歌的帮助
For more details refer some existing articles with the help of google
这篇关于在ASP.Net MVC的长时间运行的服务器的进度条调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!