Reputation: 627
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.?
Upvotes: 2
Views: 3800
Reputation: 28600
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.
Upvotes: 1
Reputation: 59041
Put this code within an HttpHandler and then link to that handler from the original page, passing in whatever information your handler needs.
Upvotes: 5
Reputation: 37225
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
Upvotes: 0
Reputation: 1024
Because you are calling Response.End, this halts the response of the page.
Upvotes: 9
Reputation: 359
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.
Upvotes: 0
Reputation: 48098
I think you should open a popup page / handler that does this Response.WriteFile operation.
Upvotes: 4