简体   繁体   中英

ASP.NET - Download File & Show on Browser

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM