1

My task is very simple, on Button Click I want to show the generated PDF on the browser but at the same time download it on the client machine.

I have made 2 functions, One for downloading & other for showing it on browser. When I run them separately they work like a charm but when I call them together on button click only the that is called first works other one just doesn't do anything.

Below is the code to download the file.

_page.Response.Clear();
            _page.Response.AddHeader("Content-Disposition", "attachment; filename=payments.xls");
            //Download the file and prompt the user to save
            _page.Response.BinaryWrite(data);
            _page.Response.End();

Below is the code to show on browser

 _page.Response.Clear();
            _page.Response.ContentType = "application/pdf";
            _page.Response.OutputStream.Write(_datastrem.GetBuffer(), 0, _datastrem.GetBuffer().Length);
            _page.Response.Flush();

**I also should mention that I want to do all that on POSTBACK.

2
  • How exactly do you call those? And have you tried showing contents in browser and using iframe from which you give your client a file? Commented Nov 26, 2013 at 11:18
  • Sorry about that, I have updated the question with code. Commented Nov 26, 2013 at 11:22

1 Answer 1

1

You don't quite show your code, but from what you described I can say that won't work: you can only perform one action per HTTP response. You either push the file for download (content-disposition) or show the file in the browser.

If you want to do both, you'll have to use an intermediate page that shows the file and then (for example using JavaScript) performs the file download.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you tell me how to download file through javascript, as brower would be showing pdf so how can I call the download function.
Show the PDF in an HTML document in an iframe, for example.
I dont want to open any popup or anything I want to shown pdf on the same page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.