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)?
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/
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With