[英]Using signalR hub from MVC action
在學習完本教程之后 ,我試圖通過長時間的操作來顯示各個步驟的進度。 根據該示例,我能夠成功地模擬集線器內的長時間操作,並在每一步將更新報告給客戶端。
更進一步,我現在想顯示具有[HttpPost]
屬性的MVC操作方法中發生的實時,長時間運行的進程的狀態。
問題是我似乎無法從集線器上下文更新客戶端。 我意識到必須創建一個中心環境才能使用該中心進行通信。 我知道的一個區別是我必須使用hubContext.Clients.All.sendMessage();
VS. hubContext.Clients.Caller.sendMessage();
在示例中列出。 根據我在《 ASP.NET SignalR集線器API指南-服務器》中的發現,我應該能夠如示例中所述使用Clients.Caller
,但僅限於在集線器類中使用它。 主要是,我只想了解為什么我不能從動作方法中得到信號。
我先感謝您的幫助。
我已經像這樣創建了我的OWIN Startup()
類。
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(HL7works.Startup))]
namespace HL7works
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
我的集線器是這樣寫的...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace HL7works
{
public class ProgressHub : Hub
{
public string msg = string.Empty;
public int count = 0;
public void CallLongOperation()
{
Clients.Caller.sendMessage(msg, count);
}
}
}
我的控制器...
// POST: /Task/ParseToExcel/
[HttpPost]
public ActionResult ParseToExcel(HttpPostedFileBase[] filesUpload)
{
// Initialize Hub context
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
hubContext.Clients.All.sendMessage("Initalizing...", 0);
double fileProgressMax = 100.0;
int currentFile = 1;
int fileProgress = Convert.ToInt32(Math.Round(currentFile / fileProgressMax * 100, 0));
try
{
// Map server path for temporary file placement (Generate new serialized path for each instance)
var tempGenFolderName = SubstringExtensions.GenerateRandomString(10, false);
var tempPath = Server.MapPath("~/" + tempGenFolderName + "/");
// Create Temporary Serialized Sub-Directory
System.IO.FileInfo thisFilePath = new System.IO.FileInfo(tempPath + tempGenFolderName);
thisFilePath.Directory.Create();
// Iterate through PostedFileBase collection
foreach (HttpPostedFileBase file in filesUpload)
{
// Does this iteration of file have content?
if (file.ContentLength > 0)
{
// Indicate file is being uploaded
hubContext.Clients.All.sendMessage("Uploading " + Path.GetFileName(file.FileName), fileProgress);
file.SaveAs(thisFilePath + file.FileName);
currentFile++;
}
}
// Initialize new ClosedXML/Excel workbook
var hl7Workbook = new XLWorkbook();
// Start current file count at 1
currentFile = 1;
// Iterate through the files saved in the Temporary File Path
foreach (var file in Directory.EnumerateFiles(tempPath))
{
var fileNameTmp = Path.GetFileName(file);
// Update status
hubContext.Clients.All.sendMessage("Parsing " + Path.GetFileName(file), fileProgress);
// Initialize string to capture text from file
string fileDataString = string.Empty;
// Use new Streamreader instance to read text
using (StreamReader sr = new StreamReader(file))
{
fileDataString = sr.ReadToEnd();
}
// Do more work with the file, adding file contents to a spreadsheet...
currentFile++;
}
// Delete temporary file
thisFilePath.Directory.Delete();
// Prepare Http response for downloading the Excel workbook
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=\"hl7Parse_" + DateTime.Now.ToString("MM-dd-yyyy") + ".xlsx\"");
// Flush the workbook to the Response.OutputStream
using (MemoryStream memoryStream = new MemoryStream())
{
hl7Workbook.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
memoryStream.Close();
}
Response.End();
}
catch (Exception ex)
{
ViewBag.TaskMessage =
"<div style=\"margin-left:15px;margin-right:15px\" class=\"alert alert-danger\">"
+ "<i class=\"fa fa-exclamation-circle\"></i> "
+ "An error occurred during the process...<br />"
+ "-" + ex.Message.ToString()
+ "</div>"
;
}
return View();
}
在“我的視圖”中(已更新以反映“詳細信息”的答案)...
@using (Html.BeginForm("ParseToExcel", "Task", FormMethod.Post, new { enctype = "multipart/form-data", id = "parseFrm" }))
{
<!-- File Upload Row -->
<div class="row">
<!-- Select Files -->
<div class="col-lg-6">
<input type="file" multiple="multiple" accept=".adt" name="filesUpload" id="filesUpload" />
</div>
<!-- Upload/Begin Parse -->
<div class="col-lg-6 text-right">
<button id="beginParse" class="btn btn-success"><i class="fa fa-download"></i> Parse and Download Spreadsheet</button>
</div>
</div>
}
<!-- Task Progress Row -->
<div class="row">
<!-- Space Column -->
<div class="col-lg-12">
</div>
<!-- Progress Indicator Column -->
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('.progress').hide();
$('#beginParse').on('click', function () {
$('#parseFrm').submit();
})
$('#parseFrm').on('submit', function (e) {
e.preventDefault();
$.ajax({
url: '/Task/ParseToExcel',
type: "POST",
//success: function () {
// console.log("done");
//}
});
// initialize the connection to the server
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);
};
// 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.callLongOperation();
});
});
});
function UpdateProgress(message, count) {
// get status div
var status = $("#status");
// set message
status.html(message);
// get progress bar
if (count > 0) {
$('.progress').show();
}
$('.progress-bar').css('width', count + '%').attr('aria-valuenow', count);
$('.progress-bar').html(count + '%');
}
</script>
<div class="col-lg-12">
<div id="status">Ready</div>
</div>
<div class="col-lg-12">
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="min-width:20px;">
0%
</div>
</div>
</div>
</div>
<!-- Task Message Row -->
<div class="row">
<div clss="col-lg-12">
@Html.Raw(ViewBag.TaskMessage)
</div>
</div>
更新:解決我的問題的方法最終是Detail的答案,但是對AJAX post方法進行了一些修改,以將文件傳遞給我的action方法。
e.preventDefault();
$.ajax({
url: '/Task/ParseToExcel',
type: "POST",
data: new FormData( this ),
processData: false,
contentType: false,
//success: function () {
// console.log("done");
//}
});
參考.. http://portfolio.planetjon.ca/2014/01/26/submit-file-input-via-ajax-jquery-easy-way/
好的,我對此進行了一些嘗試,我認為使用名為“ jQuery Form Plugin”( http://jquery.malsup.com/form )的插件會更好,它將對HttpPostedFiles有所幫助問題。
我已經采用了您的代碼,並進行了一些調整,使其正常運行。 您需要在循環的每個回合(兩個循環)中重新計算fileProgress,並且使用添加到表單中的按鈕,不再需要通過jQuery觸發發布,因此我對此進行了評論。
另外,我認為CallLongOperation()函數現在是多余的(我想只是源材料中的演示內容),因此我從集線器啟動邏輯中刪除了該調用,並用顯示按鈕的行替換了該調用-在signalR准備就緒之前,您可能應該阻止用戶開始上傳,但是signalR幾乎立即啟動,因此我認為您甚至不會注意到該延遲。
我不得不注釋掉一些代碼,因為我沒有這些對象(XLWorkbook的東西,openxml的位等),但是您應該能夠在沒有這些位的情況下運行它並跟蹤代碼以遵循邏輯,然后將這些位添加回自己。
這是一個很有趣的問題,希望我能有所幫助:)
控制器:
public class TaskController : Controller
{
[HttpPost]
public ActionResult ParseToExcel(HttpPostedFileBase[] filesUpload)
{
decimal currentFile = 1.0M;
int fileProgress = 0;
int maxCount = filesUpload.Count();
// Initialize Hub context
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
hubContext.Clients.All.sendMessage("Initalizing...", fileProgress);
try
{
// Map server path for temporary file placement (Generate new serialized path for each instance)
var tempGenFolderName = DateTime.Now.ToString("yyyyMMdd_HHmmss"); //SubstringExtensions.GenerateRandomString(10, false);
var tempPath = Server.MapPath("~/" + tempGenFolderName + "/");
// Create Temporary Serialized Sub-Directory
FileInfo thisFilePath = new FileInfo(tempPath);
thisFilePath.Directory.Create();
// Iterate through PostedFileBase collection
foreach (HttpPostedFileBase file in filesUpload)
{
// Does this iteration of file have content?
if (file.ContentLength > 0)
{
fileProgress = Convert.ToInt32(Math.Round(currentFile / maxCount * 100, 0));
// Indicate file is being uploaded
hubContext.Clients.All.sendMessage("Uploading " + Path.GetFileName(file.FileName), fileProgress);
file.SaveAs(Path.Combine(thisFilePath.FullName, file.FileName));
currentFile++;
}
}
// Initialize new ClosedXML/Excel workbook
//var hl7Workbook = new XLWorkbook();
// Restart progress
currentFile = 1.0M;
maxCount = Directory.GetFiles(tempPath).Count();
// Iterate through the files saved in the Temporary File Path
foreach (var file in Directory.EnumerateFiles(tempPath))
{
var fileNameTmp = Path.GetFileName(file);
fileProgress = Convert.ToInt32(Math.Round(currentFile / maxCount * 100, 0));
// Update status
hubContext.Clients.All.sendMessage("Parsing " + Path.GetFileName(file), fileProgress);
// Initialize string to capture text from file
string fileDataString = string.Empty;
// Use new Streamreader instance to read text
using (StreamReader sr = new StreamReader(file))
{
fileDataString = sr.ReadToEnd();
}
// Do more work with the file, adding file contents to a spreadsheet...
currentFile++;
}
// Delete temporary file
thisFilePath.Directory.Delete();
// Prepare Http response for downloading the Excel workbook
//Response.Clear();
//Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//Response.AddHeader("content-disposition", "attachment;filename=\"hl7Parse_" + DateTime.Now.ToString("MM-dd-yyyy") + ".xlsx\"");
// Flush the workbook to the Response.OutputStream
//using (MemoryStream memoryStream = new MemoryStream())
//{
// hl7Workbook.SaveAs(memoryStream);
// memoryStream.WriteTo(Response.OutputStream);
// memoryStream.Close();
//}
//Response.End();
}
catch (Exception ex)
{
ViewBag.TaskMessage =
"<div style=\"margin-left:15px;margin-right:15px\" class=\"alert alert-danger\">"
+ "<i class=\"fa fa-exclamation-circle\"></i> "
+ "An error occurred during the process...<br />"
+ "-" + ex.Message.ToString()
+ "</div>"
;
}
return View();
}
}
視圖:
@using (Html.BeginForm("ParseToExcel", "Task", FormMethod.Post, new { enctype = "multipart/form-data", id = "parseFrm" }))
{
<!-- File Upload Row -->
<div class="row">
<!-- Select Files -->
<div class="col-lg-6">
<input type="file" multiple="multiple" accept=".adt" name="filesUpload" id="filesUpload" />
</div>
<!-- Upload/Begin Parse -->
<div class="col-lg-6 text-right">
<button id="beginParse" class="btn btn-success"><i class="fa fa-download"></i> Parse and Download Spreadsheet</button>
</div>
</div>
}
<!-- Task Progress Row -->
<div class="row">
<!-- Progress Indicator Column -->
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('.progress').hide();
$('#beginParse').hide();
// initialize the connection to the server
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);
};
// establish the connection to the server
$.connection.hub.start().done(function () {
//once we're connected, enable the upload button
$('#beginParse').show();
});
//no need for this, the button submits the form
//$('#beginParse').on('click', function () {
// $('#parseFrm').submit();
//})
//ajaxify the form post
$('#parseFrm').on('submit', function (e) {
e.preventDefault();
$('#parseFrm').ajaxSubmit();
});
});
function UpdateProgress(message, count) {
// get status div
var status = $("#status");
// set message
status.html(message);
// get progress bar
if (count > 0) {
$('.progress').show();
}
$('.progress-bar').css('width', count + '%').attr('aria-valuenow', count);
$('.progress-bar').html(count + '%');
}
</script>
<div class="col-lg-12">
<div id="status">Ready</div>
</div>
<div class="col-lg-12">
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="min-width:20px;">
0%
</div>
</div>
</div>
</div>
<!-- Task Message Row -->
<div class="row">
<div clss="col-lg-12">
@Html.Raw(ViewBag.TaskMessage)
</div>
</div>
ps不要忘記在_Layout.cshtml中添加對jQuery Form Plugin的腳本引用:
<script src="http://malsup.github.com/jquery.form.js"></script>
目前尚不清楚究竟是什么問題給您,但是使用您的代碼,我做了一些改動之后就完成了這一工作。
首先,表單發布正在重新加載頁面,如果您打算為此使用POST,那么您需要通過捕獲發布事件並阻止默認操作來異步執行此操作(然后使用jQuery進行接管)。 我不確定您打算如何觸發該帖子(也許我只是在您的代碼中錯過了它),所以我添加了一個按鈕並插入其中,但是根據需要進行了更改:
<!-- Progress Indicator Column -->
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('.progress').hide();
$('#button1').on('click', function () {
$('#form1').submit();
})
$('#form1').on('submit', function (e) {
e.preventDefault();
$.ajax({
url: '/Progress/DoTest',
type: "POST",
success: function () {
console.log("done");
}
});
// initialize the connection to the server
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);
};
// 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.callLongOperation();
});
});
});
function UpdateProgress(message, count) {
// get status div
var status = $("#status");
// set message
status.html(message);
// get progress bar
if (count > 0)
{
$('.progress').show();
}
$('.progress-bar').css('width', count + '%').attr('aria-valuenow', count);
$('.progress-bar').html(count + '%');
}
</script>
<div class="col-lg-12">
<div id="status">Ready</div>
</div>
<form id="form1">
<button type="button" id="button1">Submit Form</button>
</form>
<div class="col-lg-12">
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="min-width:20px;">
0%
</div>
</div>
</div>
我還簡化了Controller,只是專注於眼前的問題。 分解問題,讓機制首先起作用,尤其是在您遇到問題時,然后再添加額外的邏輯:
public class ProgressController : Controller
{
// GET: Progress
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult DoTest()
{
// Initialize Hub context
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();
hubContext.Clients.All.sendMessage("Initalizing...", 0);
int i = 0;
do
{
hubContext.Clients.All.sendMessage("Uploading ", i * 10);
Thread.Sleep(1000);
i++;
}
while (i < 10);
return View("Index");
}
}
另外,請確保您的javascript引用順序正確,必須先加載jquery,然后加載signalr,再加載hub腳本。
如果仍然有問題,請發布確切的錯誤消息,但我懷疑這是同步表單/重新加載問題。
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.