c# - 跟踪保存在 "live"进度条中的数据库的进度 asp.net mvc

标签 c# asp.net asp.net-mvc asp.net-ajax

我需要在 .net 4.5 中显示 SQL 加载的进度。目的是展示正在发生的上传的“实时”流程。以便上传信息​​的人可以看到上传正在运行。

Controller 方法:

private void ProgressUpload(int? SystemGeneralAnnouncementId) {

                var systemGeneralAnnouncement = (SystemGeneralAnnouncementId == null) ? null : _uow.SystemGeneralAnnouncementRepository.GetById(SystemGeneralAnnouncementId.Value);
                List<Status> status = new List<Status>();

                if (systemGeneralAnnouncement.Statuses.Length > 0)
                {
                    status.AddRange(systemGeneralAnnouncement.Statuses.Split(',').Select(item => (Status)Enum.Parse(typeof(Status), item)));
                }

                var allEmailAddresses = new List<PointOfContact>();
                var EmailAddresses = new List<PointOfContact>();

                //retrieve all Point of contact based upon selected statuses per each loop
                var result = new List<PointOfContact>();
                foreach (var item in status)
                {
                    result = _uow.PointOfContactRepository.GetAllByStatus(item).ToList();
                    allEmailAddresses.AddRange(result);
                }

                // Retrieve the email addresses based on the who is intended to receive the email message
                if (systemGeneralAnnouncement.SendToRecipients.Contains("(1) All Three Contacts"))
                {
                    EmailAddresses = allEmailAddresses;
                }
                else
                {
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(2) All first Contacts"))
                    {
                        pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Primary).ToList());
                    }
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(3) All Second Contacts"))
                    {
                        pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.Secondary).ToList());
                    }
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(4) All third contacts"))
                    {
                        pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.SigningAuthority).ToList());
                    }
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(5) All fourth Contacts"))
                    {
                        pocEmailAddresses.AddRange(allEmailAddresses.Where(r => r.PointOfContactType == PointOfContactTypes.TuitionRates).ToList());
                    }
                    if (systemGeneralAnnouncement.SendToRecipients.Contains("(6) Specified Email Address"))
                    {
                        var pocs = new List<PointOfContact>();

                        string[] emails = systemGeneralAnnouncement.EmailAddresses.Split(',');

                        foreach (string email in emails)
                        {
                            var addPoc = new PointOfContact { Email = email };

                            User user = _uow.UserRepository.GetByEmail(email);

                            if (user == null)
                            {
                                add.FirstName = "Not Created Account Yet";
                            }
                            else
                            {
                                addPoc.FirstName = user.FirstName;
                                addPoc.LastName = user.LastName;
                            }

                            List<PointOfContact> idAssociatedToUser =
                                _uow.PointOfContactRepository
                                    .GetAllPocsByEmail(email)
                                    .ToList();


                            if (idAssociatedToUser.Count == 0)
                            {
                                addPoc.IDNumber = "N/A";
                            }
                            else
                            {
                                string[] idArray = idAssociatedToUser
                                    .Select(x => x.IDNumber)
                                    .ToArray();

                                addPoc.IDNumber = string.Join(",", opeidArray);
                            }
                            pocs.Add(addPoc);
                        }
                        EmailAddresses.AddRange(pocs);
                    }
                }
                // if any poc addresses were found...
                if (EmailAddresses.Count > 0)
                {
                    string emailBody = WebUtility.HtmlDecode(systemGeneralAnnouncement.EmailBody);

                    foreach (PointOfContact emailAddress in EmailAddresses.Where(x => x.Email != "" && x.Email != null).ToList())
                    {
                        string firstName = emailAddress.FirstName == null ? "" : emailAddress.FirstName.Trim();
                        string lastName = emailAddress.LastName == null ? "" : emailAddress.LastName.Trim();
                        string userName = firstName + " " + lastName;

                        //Below I Used SqlCommand vs EF because EF has AutoDetectChangesEnabled and it slows things down when adding to the context. Instead of tuning it by turning it to false or 
                        //configure it to use AddRange, SqlCommand is the best option for speed.

保存到数据库:

SaveToDatabase(emailAddress.Email, emailBody, systemGeneralAnnouncement.Subject, UserIdentityHelper.GetUserEmailAddress + " (" + UserIdentityHelper.GetUserId + ")", systemGeneralAnnouncement.SystemGeneralAnnouncementId, userName, emailAddress.IDNumber);

    LogInstitutionEmail(systemGeneralAnnouncement.Subject, emailBody, emailAddress.Email, emailAddress.IDNumber, systemGeneralAnnouncement.EmailAttachmentLocation);
                }
            }
        }

        private void LogInstitutionEmail(string subject, string emailBody, string email, string opeidNumber, string emailAttachment)
        {
            try
            {
                using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MasterContext"].ConnectionString))
                {
                    conn.Open();
                    SqlCommand cmd;
                    if (string.IsNullOrEmpty(emailAttachment))
                    {
                        cmd = new SqlCommand("Insert Into Emails (Since, Subject, Email, EmailAddress, OpeidNumber, FirstReadDateTime) VALUES(@Since, @Subject, @Email, @EmailAddress, @OpeidNumber, NULL)", conn);
                    }
                    else
                    {
                        cmd = new SqlCommand("Insert Into Emails (Since, Subject, Email, EmailAddress, OpeidNumber, FirstReadDateTime, Attachment) VALUES(@Since, @Subject, @Email, @EmailAddress, @IDNumber, NULL, @Attachment)", conn);
                        cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Attachment", Value = emailAttachment });
                    }


                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Since", Value = DateTime.Now });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Subject", Value = subject });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Email", Value = emailBody });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmailAddress", Value = email });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@idNumber", Value = idNumber });
                    cmd.CommandType = CommandType.Text;

                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            }
        }

        private void SaveToDatabase(string emailRecipient, string emailBody, string subject, string userWhoSentIt, int systemGeneralAnnouncementId, string userName, string opeidNumber)
        {
            try
            {
                using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MasterContext"].ConnectionString))
                {
                    conn.Open();
                    var cmd = new SqlCommand("Insert Into EmailQueue (EmailRecipients, EmailBody, EmailSubject, UserWhoSentIt, QueueDate, SystemGeneralAnnouncementId, UserName, OpeidNumber) VALUES(@EmailRecipients, @EmailBody, @EmailSubject, @UserWhoSentIt, @QueueDate, @SystemGeneralAnnouncementId, @UserName, @OpeidNumber)", conn);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add(new SqlParameter() {ParameterName = "@EmailRecipients", Value = emailRecipient });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmailBody", Value = emailBody });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmailSubject", Value = subject });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@UserWhoSentIt", Value = userWhoSentIt });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@QueueDate", Value = DateTime.Now });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@SystemGeneralAnnouncementId", Value = systemGeneralAnnouncementId });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@UserName", Value = userName });
                    cmd.Parameters.Add(new SqlParameter() { ParameterName = "@idNumber", Value = idNumber });


                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            }
        }

调用 Post 方法的 View :

@model DODMOU.Model.Entities.SystemGeneralAnnouncement
@{
    ViewBag.Title = "Send Email";
    Layout = "~/Views/Shared/_AdministratorLayout.cshtml";
}
<div class="title">
    <h2>Send Email</h2>
</div>
<hr />
<div class="title">
    <h2>Are you sure you want to Send this Email?</h2>
</div>
<div class="row">
    <div class="col-sm-offset-2 col-sm-9">
        <table class="table table-striped table-hover table-bordered table-responsive">
            <tr>
                <th scope="row" class="col-md-1">@Html.DisplayNameFor(model => model.SendToRecipients) :</th>
                <td class="col-md-8">@Html.DisplayFor(model => model.SendToRecipients)</td>
            </tr>
            <tr>
                <th scope="row">@Html.DisplayNameFor(model => model.Subject) :</th>
                <td>@Html.DisplayFor(model => model.Subject)</td>
            </tr>
            <tr>
                <th scope="row">@Html.DisplayNameFor(model => model.EmailBody) :</th>
                <td class="col-sm-8">@MvcHtmlString.Create(HttpUtility.HtmlDecode(Model.EmailBody))</td>
            </tr>
            <tr>
                <th scope="row">@Html.DisplayNameFor(model => model.Since) :</th>
                <td>@Html.DisplayFor(model => model.Since)</td>
            </tr>
        </table>
    </div>
    <div class="row col-sm-offset-2 col-sm-9 text-right">
        <div class="row col-sm-offset-2 col-sm-9 text-right">
            @using (Html.BeginForm("ConfirmSend", "SystemGeneralAnnouncement", FormMethod.Post))
            {
                @Html.AntiForgeryToken()
                @Html.HiddenFor(model => model.SystemGeneralAnnouncementId)
                <div class="form-actions no-color">
                    <input type="submit" value="Send" class="btn btn-primary" /> |
                    @Html.ActionLink("Back to List", "Index")
                </div>
            }
        </div>

    </div>
</div>

接收上传信息的 View :

<div class="col-xs-12" style="text-align: center">
    <p>
        <div class="alert alert-success Loading">
            <h2 class="title">General Announcement is Loading...</h2>
            <br />
        </div>
        The selected general announcements are being uploaded...<br />
        <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div> </div>
        @Html.ActionLink("Return to the System General Announcement page.", "Index", "SystemGeneralAnnouncement")
    </p>
    <div id="success" style="display:none">
        <div class="alert alert-success">
            <h2 class="title">General Announcement Successfully Generated!</h2>
        </div>
        The selected general announcement has been successfully generated and will begin sending to the appropriate recipients within the next few minutes.<br />
        @Html.ActionLink("Return to the System General Announcement page.", "Index", "SystemGeneralAnnouncement")
    </div>
</div>
@section scripts{
    <script>
        $.ajax({
            type: 'POST',
            url: "ProgressUpload, systemGeneralAnnouncement",
            data: {},
            success: function (data) {
                $(".Loading").hide();
                $("#success").css("display","block");
            },
            xhr: function () {
                var xhr = new window.XMLHttpRequest();
                xhr.upload.addEventListener("progress", function (evt) {
                    if (evt.lengthComputable) {
                        var percentComplete = evt.loaded / evt.total;

                    }
                }, false);

                return xhr;
            },

        });
    </script>
    }

如上所述,这里的最终目标是允许用户在上传文件时在页面上看到“实时”进度。成功返回将在 100% 后显示,让他们知道上传完成。

我的问题是,我缺少什么来公开要上传的当前值?它应该来自事件监听器,但不显示任何上传进度。

最佳答案

我建议使用 SignalR 将进度数据更新到客户端。 假设您的上传数据是 IEnumerable 类型,您需要在 foreach 循环中捕获每次更新到 DB 并使用 SignalR 将数据更新到客户端。

查看此链接: https://www.codeproject.com/Articles/1124691/SignalR-Progress-Bar-Simple-Example-Sending-Live-D

关于c# - 跟踪保存在 "live"进度条中的数据库的进度 asp.net mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43194188/

相关文章:

c# - DDD富域模型和聚合根

c# - ASP.NET Core 服务不创建 RabbitMQ 队列

c# - 未应用 ASP.NET href CSS

asp.net-mvc - LINQ:为什么我在此 LINQ 查询中收到错误?

c# - 迁移到 dotnet Core 3.1 后未找到 UseSpaStaticFiles()

C# WCF 动态

c# - visual-studio 2013 是否支持 Windows-mobile 或 Windows-CE 编程?

c# - ListView 中奇怪的寻呼机行为

c# - 使用 GET 将当前用户作为参数传递给外部 url

c# - 向 POCO 添加方法或创建单独的类来更新 POCO 的值是一种好的做法吗?