0

As per an ASP.NET page, on click of a button the following actions should happen:

  1. Download a file from server
  2. Perform clean up actions like

    • Hide a Button
    • Set text to a label
    • Display the label
    • Disable a button
    • etc.

Now the download part happens through the following code:

Response.Clear();               
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename="+strFileName+".pdf");
Response.WriteFile(strBrowserPath); 

And the remaining actions (Point 2) are done after.

Sadly, since Response.Clear()/Response.WriteFile() is used the remaining server side actions are not happening.

Any alternatives? Any fixes for this?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
tempidope
  • 823
  • 1
  • 12
  • 29

1 Answers1

2

From the moment you use the post back action to send a file, at the same time you can not send and the page with the changes (disable button, change text, etc). There is no fixes for this because there is not a problem, is the way its works. You have one pipeline to send your response back. You ask for one http response and you get one http response, you can not have both update page and file.

Now, alternative. First of all is how you going to send the file. I suggest to use a handler with parameters like download.ashx?Dhwoload=filea.pdf and after the post back with the update on your page, you call the

window.location = "download.ashx?Dhwoload=filea.pdf";

javascript, with an alternative link as

<a href="download.ashx?Dhwoload=filea.pdf">if the file did not start to download automatically click here</a>

and make your work.

relative:
What is the best way to download file from server
Error handling when downloading file from ASP.NET Web Handler (.ashx)

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150