【发布时间】:2019-07-25 01:14:25
【问题描述】:
我有一个在处理上传文件的页面上实现 SignalR 的进度条逻辑。它可以正常工作并产生正确的进度。
但是,它会为所有用户生成进度条,而不仅仅是上传文件的用户。换句话说,一个用户上传了文件,但该文件上传的进度甚至显示在其他用户/会话的屏幕上,这些用户/会话没有对其进行任何操作
我确实想出了一个解决方法,我发送一个带有 SignalR 进度调用/信号的用户 ID,并将其与存储在 aspx 上的隐藏字段中的用户 ID 进行比较。如果它们不匹配,我不会生成进度条。但是,此修复程序似乎是一种肮脏的解决方法。
是否有更有效的方法来确保 SignalR 仅在一个会话中工作?
以防万一这是我的代码
protected void btnSubmit_Click(object sender, EventArgs e)
{
HttpFileCollection attachments = null;
try
{
lblMessage.Text = string.Empty;
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>(); hubContext.Clients.All.AddProgress("Upload has been initiated: ", string.Empty, "0", Context.User.Identity.Name, pageName);
if (fileupload1.HasFile)
{
attachments = Request.Files;
if (attachments.Count > totalnumberoffiles)
{
lblMessage.Text += "Please select only " + totalnumberoffiles + " files.";
lblMessage.Visible = true;
}
else
{
double fileProgressPercentagePortion = 100 / attachments.Count;
double fileProgressPercentage = 0;
double fileProgressPercentageSegment = fileProgressPercentagePortion/6;
for (int i = 0; i < attachments.Count; i++)
{
HttpPostedFile attachment = attachments[i];
if (attachment.FileName == string.Empty)
{
continue;
}
hubContext.Clients.All.AddProgress("Currently processing: ", new System.IO.FileInfo(attachment.FileName).Name, "0",
Context.User.Identity.Name, pageName);
if (attachment.ContentLength > 0 && !String.IsNullOrEmpty(attachment.FileName))
{
hubContext.Clients.Client(hubContext.co).AddProgress("Currently processing: ", new System.IO.FileInfo(attachment.FileName).Name,
fileProgressPercentageSegment, Context.User.Identity.Name, pageName);
ProcessFile(attachment, hubContext, fileProgressPercentageSegment,
fileProgressPercentage);
fileProgressPercentage += fileProgressPercentagePortion;
}
}
}
}
}
catch (Exception e3)
{
}
finally
{
}
}
这是我的 JavaScript
$(function () {
// Reference the auto-generated proxy for the hub.
var progress = $.connection.progressHub;
console.log(progress);
var hfUserAccount = document.getElementById("<%=hfUserAccount.ClientID %>");
// Create a function that the hub can call back to display messages.
progress.client.AddProgress = function (fileName, message, percentage, userAccount, pageName) {
if (userAccount === hfUserAccount.value && pageName === "CheckEFile.aspx") {
ProgressBarModal("show", fileName + " " + message);
document.getElementById("divProgress").style.display = "block";
document.getElementById("divUpload").style.display = "block";
document.getElementById("divProgress").style.width = percentage + "%";
document.getElementById("lblPercentage").innerHTML = parseInt(percentage) + "%";
$("#processingStatus").html("Please Wait. Checking files...");
$('#ProgressMessage').width(percentage);
if (percentage === "100%") {
ProgressBarModal();
}
}
};
$.connection.hub.start().done(function () {
var connectionId = $.connection.hub.id;
console.log(connectionId);
});
});
这里是我的中心
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace IAACCESS.SignalR
{
public class ProgressHub : Hub
{
static ProgressHub()
{
}
}
}
【问题讨论】:
-
您可以只使用
Clients.Caller只响应调用您方法的人 -
谢谢@MindSwipe,但是,由于某种原因,我在 asp.net SignalR 中无法使用“Caller”属性。我应该使用其他属性吗?感谢您的反馈
-
不清楚您在何时何地为您的客户调用 AddProgress 事件。你能发送“hubContext.Clients.All.AddProgress(..)”调用所在的代码块吗?
-
@NthDeveloper 刚刚发了一个完整的事件方法,谢谢
标签: c# asp.net webforms signalr