2

When submitting a form (form.submit() and without ajax), is there any way to detect if a response came back (assuming a new page isn't loaded). From the controller I'm actually returning a file rather than a new View.

View:

<% using (Html.BeginForm()){%>
   ....
   <input id="submitsearch" type="submit" value="DownloadFile" name="SubmitButton" />
<%} %>

Controller:

return File(FileContent, "text/plain", Filename);

Basically what I want to happen is that when the user clicks submit, I display a loading icon and when the download popup appears I want to remove the loading icon.

So I do not actually need to read the response, but just know when the response comes back so i can remove the loading icon.

Restriction is that I cannot use an ajax call to submit the page.

Cheers.

FruitDealer
  • 91
  • 2
  • 8

2 Answers2

5

You could use a technique which I call cookie polling:

<% using (Html.BeginForm("Download", "Home")) { %>
    <%= Html.Hidden("downloadToken", DateTime.Now.Ticks) %>
    <input type="submit" value="Download" />
<% } %>

<script src="<%= Url.Content("~/Scripts/jquery.cookie.js") %>" type="text/javascript"></script>
<script type="text/javascript">
    $('form').submit(function () {
        // We start a download => show some progress indicator here
        $(':submit', this).val('Please wait while downloading...').attr('disabled', 'disabled');

        // start polling for the cookie every 500ms
        var fileDownloadCheckTimer = window.setInterval(function () {
            var cookieValue = $.cookie('fileDownloadToken');
            var token = $('#downloadToken').val();
            if (cookieValue == token) {
                // The download has finished => remove the progress indicator
                $(':submit', $('form')).val('Download').removeAttr('disabled');

                // remove the cookie
                $.cookie('fileDownloadToken', null);

                // stop polling
                window.clearInterval(fileDownloadCheckTimer);
            }
        }, 500);
    });
</script>

and inside the controller action:

public ActionResult Download(string downloadToken)
{
    // Simulate a slow download
    Thread.Sleep(5000);
    var cookie = new HttpCookie("fileDownloadToken", downloadToken);
    // set the cookie with the proper value
    Response.AppendCookie(cookie);
    return File(Encoding.UTF8.GetBytes("foo bar"), "text/plain", "foo.txt");
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

If you can't use ajax you could just return a View with a link to the file download. But without ajax you will have to do a full page reload...

daniel.herken
  • 1,095
  • 7
  • 19
  • Hi,actually if you returning a file the page does not refresh at all. Everything is working fine (ie. the download), I just need to know when to remove the loading icon. – FruitDealer Jun 30 '11 at 07:06
  • Maybe this would help you then: http://stackoverflow.com/questions/1218245/jquery-submit-form-and-then-show-results-in-an-existing-div – daniel.herken Jun 30 '11 at 07:15