2

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.?

janhartmann
  • 14,713
  • 15
  • 82
  • 138
Guddu
  • 627
  • 3
  • 13
  • 28

6 Answers6

9

Because you are calling Response.End, this halts the response of the page.

Andrew Corkery
  • 1,024
  • 1
  • 8
  • 13
  • 1
    Yes. But because the content type of the response has been changed from "text/html" to "application/octet-stream" there will be no output rendered in the browser. I would also suggest using a handler to serve out the file - e.g. you could pass a Guid on the query string to identify the desired file. – Andrew Corkery Apr 22 '09 at 12:47
5

Put this code within an HttpHandler and then link to that handler from the original page, passing in whatever information your handler needs.

Haacked
  • 58,045
  • 14
  • 90
  • 114
  • Gosh, there's an awful lot of wrangling that seems to occur around this issue. The simple HttpHandler solution seems to be the straightforward and proper way to solve this. – Paul Prewett Aug 11 '09 at 12:51
4

I think you should open a popup page / handler that does this Response.WriteFile operation.

Canavar
  • 47,715
  • 17
  • 91
  • 122
1

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.

Community
  • 1
  • 1
Jason
  • 28,040
  • 10
  • 64
  • 64
0

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.

Gustavo
  • 359
  • 2
  • 5
0

You cannot answer a single request (i.e. 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

Community
  • 1
  • 1
devio
  • 36,858
  • 7
  • 80
  • 143