JQuery to re-enable the form after returning the file result

I have an ASP.NET MVC form that returns a report as the result of a file (...). This means that the browser stays on the current page after starting the file download. Using jQuery, I would like to disable the submit button and then turn it back on after the file returns . My code already disables the button just fine, however I can not find the event to connect to it, which is fired after the file is returned.

Here is what I still have:

  $(document).ready(function() {
        $("#downloadForm").submit(function() {
            $("#cmdSubmit").attr("disabled", "disabled");
            $("#uiProgress").show();
        });
    });

What can I add to this to call when the form returns?

+3
source share
3 answers

, : , , #downloadForm AJAX, URL- . , document.location URL.

+3

, !

+2

, , , ASP.NET MVC.

, , cookie .

( VB.NET)

:

Public Property DownloadToken As String

cookie :

If Response.Cookies("MyFileDownloadToken") Is Nothing Then
    Dim cookie As New HttpCookie("MyFileDownloadToken", ParameterModel.DownloadToken)

    Response.Cookies.Add(cookie)
Else
    Response.Cookies("MyFileDownloadToken").Value = ParameterModel.DownloadToken
End If

:

@Html.HiddenFor(Function(m) m.DownloadToken)

JavaScript sumbit jQuery Cookie, cookie.

var fileDownloadChecker;

$(function ()
{
    $("#MyForm").submit(function ()
    {
        $(".validation-summary-errors").empty(); //Clear any validation errors from a previous failed form submission
        PreventResubmission();
    });

    $("#submitButton").prop("disabled", false);
}

function PreventResubmission()
{
    //Use the current time as a unique token that we can check for in the response
    var token = new Date().getTime();

    //Set the token so that it is passed through to the controller
    $("#DownloadToken").val(token);

    //Prevent the user from resubmitting the form
    $("#submitButton").prop("disabled", true);
    $("#submitButton").val("Running...");

    //Check once a second for the token in the response
    fileDownloadChecker = window.setInterval(function ()
    {
        var cookieValue = $.cookie("MyFileDownloadToken");

        if (cookieValue == token)
        {
            EnableFormSubmission();
        }
    }, 1000);
}

function EnableFormSubmission()
{
    //Stop checking for the token
    window.clearInterval(fileDownloadChecker);
    fileDownloadChecker = 0;

    //Clear the hidden field
    $("#DownloadToken").val("");

    //Clear the cookie that contains the token
    $.cookie("MyFileDownloadToken", null, { path: "/" });

    //Enable the form submit button
    $("#submitButton").prop("disabled", false);
    $("#submitButton").val("Run");
}

:

1) HttpCookie jQuery Cookie . HttpCookie root ('/'), jQuery ( ) cookie . cookie, . , , cookie. ( , , , cookie )

2) , , , - , "" .

3) , , ​​ , . , ".validation-summary-errors".

+1

Source: https://habr.com/ru/post/1710031/


All Articles