简体   繁体   English

当表单请求导致文件下载时,如何在表单请求完成之前阻止按下按钮

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

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: 在我的Asp.net webforms网站上,我有一个表单,用户可以选择各种选项,这些选项在回发中发回,生成PDF报告并通过以下代码将该文件发送回用户进行下载:

    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. 问题是由于需要大量的数据和处理,这个报告可能需要10秒左右才能生成,但我不希望人们不耐烦并一遍又一遍地点击按钮。

In a normal page I would add javascript to disable the buttons on click. 在普通页面中,我会添加javascript来禁用点击按钮。 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. 但是,由于表单的响应不是HTML页面而是下载文件,我不知道如何检测。

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)? 基本上,如何从服务器获取响应(并启动http文件传输)后,如何禁用表单的按钮但重新启用它们?

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. 尝试使用cookie监视器,将cookie设置为下载响应的一部分,并在主页面中监视该cookie的存在。

http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/ 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. 为了获得更好的UI体验,您可能希望在提交表单之前触发AJAX调用,以检查先前的进程是否正在运行。 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? 思考?

//For disabling the button write the below code on page load. //要禁用按钮,请在页面加载时写下面的代码。

    System.Text.StringBuilder sbValid = new System.Text.StringBuilder();
    sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");
    sbValid.Append("if (Page_ClientValidate() == false) { return false; }} ");
    sbValid.Append("this.value = 'Saving..';");
    sbValid.Append("this.className  = 'Save_Button_Active';");
    sbValid.Append("this.disabled = true;");
    sbValid.Append(this.Page.GetPostBackEventReference(this.btnName));
    sbValid.Append(";");
    this.btnName.Attributes.Add("onclick", sbValid.ToString());
    this.btnName.Attributes.Add("onclick", "this.className  = 'Cancel_Button_Active';");

YOu can also refer to following links for more information: 您也可以参考以下链接获取更多信息:

http://bytes.com/topic/c-sharp/answers/887893-disabled-button-calling-even-handler-button-click http://bytes.com/topic/c-sharp/answers/887893-disabled-button-calling-even-handler-button-click

Button disable function after clicking on it 点击后按钮禁用功能

Disable the asp button after first click because it save multiple records in slow connection 首次单击后禁用asp按钮,因为它以慢速连接保存多个记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在FORM POST请求C#中下载文件 - Download a file in FORM POST request C# 如何防止请求中的“A potentially dangerous Request.Form” - How to prevent "A potentially dangerous Request.Form" in a request 如何避免潜在危险的Request.Form值? - How do I avoid a potentially dangerous Request.Form value? 文件下载完成后,我怎么知道使用httpwebrequest和webresponse? - How do i know using httpwebrequest and webresponse when a file download is completed? Request.Params和Request.Form何时不同? - When do Request.Params and Request.Form differ? 在C#中,如何防止多次按下PreviewKeyDown事件,直到特定代码完成? - In C# How do I prevent a PreviewKeyDown Event from being presses multiple times until the certain code has complete? 显示另一张表格时,如何防止原始表格失去焦点? - How do I prevent the original form from losing focus when I show another form? 如何防止从表单多次发送登录请求 - How to prevent sending login request multiple times from a form 按下按钮时如何打开另一个 window(表单)? - How do I open another window (Form) when a button is pressed? 单击按钮时,将打开一个新表单。 当我点击另一个表单时,如何将此表单保留在前台? - When I click a button, a new form opens. How do I keep this form in the foreground when I click on another form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM