简体   繁体   中英

Page Refresh in ASP.NET

save dialog saves file to the local machine. But after that, my page stand there and do nothing for the rest of my process. I use below code to open a save dialog

protected void lnkbtnDownload_Click(object sender, EventArgs e)
{
  string fileName = startupPath + "bin\\Inbox.mdb";
  System.IO.FileInfo targetFile = new System.IO.FileInfo(fileName);

  if (targetFile.Exists)
  {
      Response.Clear();
      Response.AddHeader("Content-Disposition", "attachment; filename=" + targetFile.Name);
      Response.AddHeader("Content-Length", targetFile.Length.ToString());
      Response.ContentType = "application/octet-stream";
      Response.WriteFile(targetFile.FullName);                        
      Response.End();
  }
}

the html code is :

<asp:Button id="lnkbtnDownload" runat="server" CausesValidation="false" 
  Text="Download" CssClass="buttonstyle"  OnClick="lnkbtnDownload_Click"></asp:Button>

but after the file is save to local machine and the save dialog is close, my page no response at all. May I know how to do a postback to the page after the save dialog is close.?

因为您正在调用Response.End,所以这会暂停页面的响应。

将此代码放在HttpHandler中,然后从原始页面链接到该处理程序,传入处理程序所需的任何信息。

我认为你应该打开一个执行此Response.WriteFile操作的弹出页面/处理程序。

Mark Brackett's answer to a similar question should work here, except you don't need the cross-page postback url attribute:

<script type="text/javascript">
   var oldTarget, oldAction;
   function newWindowClick(target) {
      var form = document.forms[0];
      oldTarget = form.target;
      oldAction = form.action;
      form.target = target;

      window.setTimeout(
         "document.forms[0].target=oldTarget;"
         + "document.forms[0].action=oldAction;", 
         200
      );
   }
</script>

<asp:LinkButton runat="server" id="lnkbtnDownload"
  CausesValidation="false" Text="Download" CssClass="buttonstyle" 
  OnClick="lnkbtnDownload_Click"
  OnClientClick="newWindowClick('download');" />

This will cause the postback to occur in a new window, and your existing Response handling will take care of the download. The original window form is restored for future interaction/postbacks.

I'd say you can run this code inside an iframe or you can open a popup for triggering the file download. In this case you are overwriting the Response and the page you was expected to get loaded is lost.

So, I would move this code into a dedicated page and implement one of the two solutions mentioned above.

You cannot answer a single request (ie button postback) with 2 responses.

You can however change the postback to redirect to a separate download/confirmation page, which in turn initiates the download using an iframe.

See this question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM