I am trying to implement a feature where user uploads a file, server processes the file and and displays realtime notification to the client about the status of processing e.g. validated, imported etc. There are examples where the client sends data from textboxes to the javascript proxy.
$("#btnTest").click(function () {
myHub.testMethodOnHub($("#txtEmail").val());
});
I need to send binary data to the server via signalR so that server can process that data and notify client using SignalR.
EDIT :- I was able to call a JS method from aspx.cs to set the status client side - realtime. However, the message disappears after page finishes loading.
What I have tried :-
ASPX :-
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="MultipleStepsUsingSignalR._Default" %>
<script src="Scripts/jquery-1.6.4.js" type="text/javascript"></script>
<script src="Scripts/json2.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR-0.5.2.js" type="text/javascript"></script>
<script src="signalr/hubs" type="text/javascript"></script>
<body>
<form id="Form1" runat="server">
<p>
<asp:FileUpload ID="fileUpload" runat="server" />
<asp:Button ID="btnProcessFile" runat="server" Text="Process File"
onclick="btnProcessFile_Click" />
<asp:HiddenField ID="hdnConnectionId" runat="server"/>
<%--<asp:Label Text="" ID="lblStatus" runat="server" />--%>
<span id="lblStatus"></span>
</p>
</form>
<script type="text/javascript">
$(document).ready(function () {
var multipleStepsHub = $.connection.multipleStepsHub;
multipleStepsHub.MethodInJavascript = function (status) {
$('#lblStatus').append(status);
};
$.connection.hub.start().done(function () {
$('#<%= hdnConnectionId.ClientID %>').val($.connection.hub.id);
alert($('#lblStatus').text());
});
});
</script>
</body>
Hub
[HubName("multipleStepsHub")]
public class MultipleStepsHub : Hub
{
public void ExecuteMultipleSteps(string status)
{
}
}
Code-behind for aspx
protected void btnProcessFile_Click(object sender, EventArgs e)
{
string connectionId = hdnConnectionId.Value;
var context = GlobalHost.ConnectionManager.GetHubContext<MultipleStepsHub>();
byte[] dataFromPostedFile = GetDataFromUploadedFile(fileUpload.PostedFile.InputStream);
context.Clients[connectionId].MethodInJavascript("<br>File updloaded.");
Thread.Sleep(5000);
context.Clients[connectionId].MethodInJavascript("<br>Processing step1");
Thread.Sleep(5000);
context.Clients[connectionId].MethodInJavascript("<br>Processing step2");
Thread.Sleep(5000);
context.Clients[connectionId].MethodInJavascript("<br>Processing step3");
}
What do I expect :- Status text changing to "Processing step1" then "Processing step2" and then "Processing step3"
What is the result :-
a) There is a change in the status periodically (after Thread.Sleep), however, when the page finishes, the status disappears. As you notice in the below, "Processing step1" and "Processing step2" do appear, but later disappear.