Jquery 如何从FileSystemWatcher生成引导警报
我有一个Asp.NETRazor页面,它使用FileSystemWatcher在将新文件添加到特定目录时生成事件。它工作正常,我能够生成控制台消息Jquery 如何从FileSystemWatcher生成引导警报,jquery,asp.net-core,bootstrap-4,Jquery,Asp.net Core,Bootstrap 4,我有一个Asp.NETRazor页面,它使用FileSystemWatcher在将新文件添加到特定目录时生成事件。它工作正常,我能够生成控制台消息 public void OnGet() { var directoryToWatch = @"c:\\psdata\\in"; if (!Directory.Exists(directoryToWatch)) { Console.WriteLine($"ERROR: {
public void OnGet()
{
var directoryToWatch = @"c:\\psdata\\in";
if (!Directory.Exists(directoryToWatch))
{
Console.WriteLine($"ERROR: {directoryToWatch} does not exist");
}
else
{
var inputFileWatcher = new FileSystemWatcher(directoryToWatch);
inputFileWatcher.IncludeSubdirectories = false;
inputFileWatcher.Created += FileCreated;
inputFileWatcher.Error += WatcherError;
inputFileWatcher.EnableRaisingEvents = true;
}
}
private static void FileCreated(object sender, FileSystemEventArgs e)
{
//Instead of writing to console, how to raise the #btnSubmit event that will cause the bootstrap alert to be generated?
Console.WriteLine($"* File created: {e.Name} - type: {e.ChangeType}");
}
但是,我不知道如何生成一条引导警报消息来宣布文件已创建的事实。以下是我的标记:
$(文档).ready(函数(){
$('#btnsupmit')。单击(函数(){
$('myAlert')。显示('fade');
setTimeout(函数(){
$('myAlert')。隐藏('fade');
}, 2500);
});
$('#linkClose')。单击(函数(){
$('myAlert')。隐藏('fade');
});
});代码>
数据处理机
提交
警惕一个文件刚刚添加到psData目录
尝试使用signalR生成从服务器到客户端的引导警报
SignalR服务器库包含在Microsoft.AspNetCore.App
metapackage中。不会自动将包含在项目中。对于本教程,将使用库管理器(LibMan)从UNPGG获取客户端库。unpkg是一个内容交付网络(CDN)),可以交付在Node.js包管理器npm中找到的任何内容
创建一个信号集线器,作为处理客户机-服务器通信的高级管道
public class NotificationHub:Hub
{
}
配置信号器
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSignalR(routes =>
{
routes.MapHub<NotificationHub>("/notificationHub");
});
app.UseMvc();
}
参考:及
@section Scripts {
<script src="~/lib/signalr/dist/browser/signalr.js"></script>
<script type="text/javascript">
var connection = new signalR.HubConnectionBuilder()
.withUrl("/notificationHub")
.build();
connection.on("Notify", function (message) {
appendMessage(message)
});
connection.start();
function appendMessage(content) {
alert(content);
}
</script>
}
private readonly IHubContext<NotificationHub> _hubContext;
public CreateFileModel(IHubContext<NotificationHub> hubContext)
{
_hubContext = hubContext;
}
private void FileCreated(object sender, FileSystemEventArgs e)
{
_hubContext.Clients.All.SendAsync("Notify", $"A file has just been added to the psData directory");
//Console.WriteLine($"* File created: {e.Name} - type: {e.ChangeType}");
}