简体   繁体   English

ASP .NET Core更新页面服务器端,无需刷新

[英]Asp .Net core Update Page server side without refresh

Here's my question : 这是我的问题:
It is possible to update a page server side whitout reloading it ? 是否可以在重新加载页面服务器端的情况下更新它?
Let's me explain : 让我解释一下:
I've a page with a form in it where the user upload a .csv with many row to add to the database. 我有一个带有表单的页面,用户在其中上载具有许多行的.csv文件以添加到数据库中。 But the inserts take some time and I would like to implement a bootstrap progress bar but I cannot figure how to update the progress bar value from the controller. 但是插入操作需要花费一些时间,我想实现一个自举进度条,但是我无法弄清楚如何从控制器更新进度条的值。

It is possible. 有可能的。 You could use SignalR (or other libraries) for push notifications to the client. 您可以使用SignalR(或其他库)向客户端推送通知。

  1. Upload the file and return a token/id 上传文件并返回令牌/ ID
  2. Subscribe to a SignalR Hub using that token 使用该令牌订阅SignalR集线器
  3. Meanwhile the server processes the file and publishes the progress using the SignalR hub and your token. 同时,服务器使用SignalR集线器和令牌处理文件并发布进度。
  4. Your client receives those notifications from the hub and updates the progress accordingly. 您的客户从中心收到这些通知,并相应地更新进度。

I finally just use some Ajax and progress object. 我终于只使用了一些Ajax和progress对象。
Here's the code if someone need it : 这是如果有人需要的代码:
ProgressHandler.cs ProgressHandler.cs

public class ProgressHandler
{
    public int Progress { get; set; }
    public int MaxProgress { get; set; }
    public int Percentage { get; set; }
    public String PercentageStr { get; set; }
    public String[] Events { get; set; }


    public String Statut { get; set; }
    public static ProgressHandler PH { get; set; }
    public ProgressHandler()
    { }
    public ProgressHandler(int maxProgress)
    {
        MaxProgress = maxProgress;
        PH = this;
    }

    public static ProgressHandler Current()
    {
        return PH;
    }

    public void Init()
    {
        Progress = 0;
        MaxProgress = 0;
        Statut = "Waiting";
    }

    public int GetPercentage()
    {
        if (MaxProgress != 0)
        {
            return (int)(((float)Progress / (float)MaxProgress) * 100);
        }
        else
        {
            return 0;
        }
    }

    public int EventSize()
    {
        if(Events!=null)
        {
            return Events.Count();
        }
        return 0;
    }

}

Insert Function : 插入功能:

public FileContentResult AddEmployeeListSubmit(AddEmployeeList model)
    {
        //working with file
        _progressHandler.MaxProgress = emps.Count;
        _progressHandler.Statut = "Running";
        //working with file and insert...
        int iter = 0;
        foreach(var Employee in EmployeeList)
        {
        //make insert in db
        iter++;
        _progressHandler.Progress = iter;
        }
        _progressHandler.Statut = "Complete";
        _progressHandler.Events = Errors.ToArray();
        return something;
}

InsertPage.cshtml InsertPage.cshtml

<div id="updateBar" hidden>
            <h1>Update in progress : <span class="label label-info" id="progressDisp"></span></h1>
            <div class="progress">
                <div class="progress-bar progress-bar-striped active" id="insertProgress" role="progressbar"
                     aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%">
                    0%
                </div>
            </div>
        </div>

And JS to get progress : 和JS取得进展:

$(document).ready(function () {
    var myInterval = setInterval(function () {
        getProgress();
    }, 500);
});
function getProgress() {
    $.ajax({
        type: "POST",
        data: "",
        url: "/Login/GetProgress",
        dataType: 'json',
        contentType: false,
        processData: false,
        success: function (response) {
            if (response.statut == "Running")
                $("#updateBar").slideDown();
            if (response.statut == "Complete") {
                $("#progressDisp").text("Complete").removeClass("label-info").addClass("label-success");
                $("#insertProgress").css("width", "100%").attr('aria-valuenow', 100).text("100%");

                if (response.events.length > 0 && !$("#insertError").is(":visible")) {
                                       $.each(response.events, function (key, value) {
                        console.log(key + ":" + value);
                        $("#insertError").html($("#insertError").html()+"<br><strong> Insert Error </strong>" + value);
                    });
                    $("#insertError").slideDown();
                }
            }
            else {

                $("#insertProgress").css("width", response.percentage + "%").attr('aria-valuenow', response.percentage).text(response.percentageStr + "%");
                $("#progressDisp").text(response.progress + "/" + response.maxProgress);
            }
        },
        error: function (response) {
            console.log("error")   
        }

    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net:更新页面内容而不刷新(部分页面更新) - asp.net: Update page contents without refresh (Partial Page Update) 更新面板 asp.net - 页面刷新 - Update panel asp.net - page refresh 如何在 ASP.NET CORE 与 Blazor 数据库更新后刷新 web 页面 - How to refresh web page after database update in ASP.NET CORE with Blazor 如何使用 Ajax 在 Asp.net 核心 MVC 中刷新数据而不重新加载页面? - How to refresh data without page reloading in Asp.net core MVC using Ajax? 在 asp.net 内核中使用 javascript/jquery 刷新 razor 页面而不重新加载 - Refresh razor page without reload using javascript/ jquery in asp.net core 在 asp.net 核心中从侧边栏导航而不重新加载页面? - Navigate from a side bar without page reload in asp.net core? ASP.NET Core 2.0仅在服务器端访问本地文件夹而不暴露于Web - ASP.NET Core 2.0 Access local folder only on server side without exposing to web 在没有刷新页的情况下在asp.net中插入更新的记录时,如何自动更新Gridview? - how can i update Gridview automatically when a record inserted of updated in asp.net without refresh page? ASP.NET MVC:如何在不刷新页面的情况下将事件发送回服务器 - Asp.net mvc: how to send event back to server without page refresh 在更新面板中的ASP.Net AjaxControlToolkit AsyncFileUpload在服务器端代码中使用母版页c#创建 - ASP.Net AjaxControlToolkit AsyncFileUpload in update panel that is created in server-side code with master page c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM