【问题标题】:Side actions not happening after Response.WriteFile()Response.WriteFile() 后未发生附带操作
【发布时间】:2013-02-07 06:27:02
【问题描述】:

根据 ASP.NET 页面,单击按钮时应发生以下操作:

  1. 从服务器下载文件
  2. 执行清理操作,例如

    • 隐藏按钮
    • 将文本设置为标签
    • 显示标签
    • 禁用按钮

现在下载部分通过以下代码进行:

Response.Clear();               
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename="+strFileName+".pdf");
Response.WriteFile(strBrowserPath); 

其余的动作(第2点)在之后完成。

遗憾的是,由于使用了 Response.Clear()/Response.WriteFile(),其余的服务器端操作没有发生。

还有其他选择吗?有什么解决办法吗?

【问题讨论】:

    标签: c# asp.net .net


    【解决方案1】:

    从您使用回发操作发送文件的那一刻起,同时您不能发送带有更改(禁用按钮、更改文本等)的页面。没有解决此问题,因为没有问题,是它的工作方式。您有一个管道可以将您的响应发回。你要求一个 http 响应,你得到一个 http 响应,你不能同时拥有更新页面和文件。

    现在,另一种选择。首先是您将如何发送文件。我建议使用带有 download.ashx?Dhwoload=filea.pdf 之类参数的处理程序,然后在您的页面上发布更新后,您调用

    window.location = "download.ashx?Dhwoload=filea.pdf";

    javascript,带有替代链接

    <a href="download.ashx?Dhwoload=filea.pdf">if the file did not start to download automatically click here</a>

    并做好你的工作。

    亲戚:
    What is the best way to download file from server
    Error handling when downloading file from ASP.NET Web Handler (.ashx)

    【讨论】:

      猜你喜欢
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      相关资源
      最近更新 更多