Safirio
Safirio

Reputation: 125

ASP.NET - How to postback a page after a file download?

Here my situation :

The user click on a LinkButton, and the page does a PostBack. But I also need to prompt a file download to the user at the same time.

In order to do that, I did this on the LinkButton

lnkPrint.Attributes.Add("onclick", "window.open('Download.ashx?type=x')");

The Download.ashx Http Handler generates the file (Content-Type : application/pdf), and if I click on my LinkButton, it does PostBack and download the file after showing a popup... But I can't manage to close this popup automatically.

I tried some methods

So, none of these methods seems to work fine.

So, my question is : Is there a way to make the popup instantly disappear, or is there a means to make the page download the file AND postback at the same time ?

PS : I thought of the Your download will begin shortly method, but I'm afraid I'll have the same issues as before with the RegisterStartupScript...

Upvotes: 2

Views: 2740

Answers (2)

krisdyson
krisdyson

Reputation: 3255

Why not do a normal postback, but then make your response, the file download? Just do Response.BinaryWrite()... and use Response.ContentType Also, you can set the content-disposition header to the filename of your choice.

Upvotes: 0

Michael
Michael

Reputation: 20049

If you want to close the window immediately, do you really need the window at all? Can't you just do:

<a href="Download.ashx?type=x">Download</a>

Then there is no popup, and if the content disposition is set to attachment, you won't redirect the user to a blank page.

Upvotes: 3

Related Questions