Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent button presses until form request is completed when form request causes a file download

Tags:

c#

asp.net

In my Asp.net webforms site I have a form where users select various options and those options are sent back in a postback that generates a PDF report and sends that file back to the user for download via the following code:

    protected void btnTopGenReport_Click(object sender, EventArgs e)
    {
        var stream = new PodMainReportGenerator().GenerateReport(GetReportParameters());

        var bytes = stream.ToArray();
        stream.Close();

        // Set the content headers
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment;filename=testReport.pdf");
        Response.AddHeader("Content-Length", bytes.Length.ToString());

        Response.BinaryWrite(bytes);

        Response.End();
    }

The problem is this report can take a good 10 or so seconds to generate due to the amount of data and processing required, but I don't want people getting impatient and clicking the button over and over again.

In a normal page I would add javascript to disable the buttons on click. This works because when postback is complete the server comes back with the form buttons re-enabled. However, since the form's response is not an HTML page but a downloaded file, which I don't know how to detect.

Essentially, how do I disable the form's buttons but re-enable them once we get the response from the server (and the http file transfer is initiated)?

like image 509
KallDrexx Avatar asked Oct 19 '12 15:10

KallDrexx


2 Answers

You just need an indicator outside of the response content to notify you that the download is complete. Try using a cookie monitor, where you set the cookie as part of the download response, and in your main page, monitor for the existence of that cookie.

http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/

like image 73
jvenema Avatar answered Oct 15 '22 01:10

jvenema


Trying to think outside the box here. Instead of disabling/enabling the submit button, maybe you can set a session level variable indicating that the report is running. If the user clicks the submit button again while a previous request is being processed, do not start a new process.

For a better UI experience, you might want to have a AJAX call fired off before the form is submitted to check if a previous process is running. If it is, you can cancel the form submission and display a message along the lines of "Your previous request is still being processed."

Thoughts?

like image 2
Shai Cohen Avatar answered Oct 15 '22 02:10

Shai Cohen