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/