tags:

views:

1135

answers:

6

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

+4  A: 

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

Canavar
target="_blank" <- You can add this to your LinkButton (even though visual studio will give you squigglies
Martin Murphy
A: 

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
+9  A: 

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

Andrew Corkery
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
A: 

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

devio
+5  A: 

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

Haacked
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
+1  A: 

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.

Jason