2017-04-03 96 views
3

我需要在.net 4.5中显示SQL负载的进度。目标是显示上传的“实时”流程。以便上传信息​​的人可以看到上传正在运行。跟踪数据库保存在“活动”进度条中的进度asp.net mvc

控制器的方法:

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方法:

@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> 

查看接收信息上传:

<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%之后显示,以让他们知道上传已完成。

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

回答