【问题标题】:How to prevent SignalR client reconnection after ajax-post request?ajax-post请求后如何防止SignalR客户端重新连接?
【发布时间】:2019-10-19 20:31:43
【问题描述】:

我将 SignalR 用于实时 aspnet 应用程序。用户应通过表单上传文件。

我使用 JQuery 代码进行上传 - 它向控制器方法发送 ajax 后请求:

<form id="uploader">
    <label>Upload file:</label>
    <input name="file" id="fileInput" type="file" multiple>
    <input type="submit" value="Upload and check" id="check" />
</form>
$("#check").click(function () {
        $("#uploader").ajaxSubmit({ url: '/Home/UploadAjax', type: 'post' });
    })
public void UploadAjax()
        {
           for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase file = Request.Files[i]; 
                int fileSize = file.ContentLength;
                string fileName = file.FileName;
                string mimeType = file.ContentType;
                System.IO.Stream fileContent = file.InputStream;

                file.SaveAs($@"{userFolderPath}\{fileName}"); 
                Temp.certPath = $@"{userFolderPath}\{fileName}";
            }

        }

但是在 UploadAjax() 方法结束后,signalR 重新连接到服务器。为什么会发生以及如何预防?

【问题讨论】:

    标签: asp.net-mvc signalr


    【解决方案1】:

    问题出在输入类型上。 submit 导致页面刷新。 更改为button 即可解决问题:

    <input type="button" value="Upload and check" id="check" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多