Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3935684
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:51:30+00:00 2026-05-19T23:51:30+00:00

I have a button that when clicked, will generate a PDF and write it

  • 0

I have a button that when clicked, will generate a PDF and write it out to the response.

This is the basic structure of the code:

try
{
    using(Stream stream = generatePdf())
    {
       var file = createFile(stream);
       file.Transmit(HttpContext.Current.Response);
    }
}
catch (Exception ex)
{
    // Handle exception
    // Display Error Message
}

The transmit method contains the following code:

response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", filename));
response.AddHeader("Content-Length", bytes.Length.ToString());
response.ContentType = "application/octet-stream";
response.BinaryWrite(bytes);

Downloading the file works fine, except that it doesn’t complete the postback.

If I were to throw an exception above file.Transmit, the error handling would work properly and I would see the message get displayed in my browser. However, if there is an exception after the file.Transmit then nothing happens. The user saves/opens the pdf and the page does not reload.

How can I make it so that the postback always completes, that way I can display an appropriate message to the user?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:51:30+00:00Added an answer on May 19, 2026 at 11:51 pm

    It sounds like you’re trying to make the response contain two parts: the page and the PDF. You can’t do that. Typically download pages start a download as a separate request when you’ve gone to them (via JavaScript, I believe), with a direct link just in case the JavaScript doesn’t work.

    Any one HTTP request can only have one response.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a button that, when clicked, will execute rails code. Is
I have a button that when clicked will run a stored procedure on a
I have a button in my XML, that when clicked will toggle the visibility
Alright, so I have a button that when clicked, will change the class of
I have a button that when clicked will search the market for an app.
In my Silverlight UI, I have a button that when clicked pops up a
In my Delphi Project i want to have a 'Settings' button that when clicked,
I have a button that opens a dialog when clicked. The dialog displays a
I have a usercontrol that hides a div when a button is clicked. <asp:LinkButton
I'm trying to create a button that, when clicked will cycle ad nauseum through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.