user194076
user194076

Reputation: 9027

Response.AddHeader attachment doesn't work

here's the code I have:

                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.BinaryWrite(pck.GetAsByteArray());
                Response.End();

The problem is that when this code is run (On button click) I do not a file for download in my browser (tried in Chrome \ IE).

pck is an excel file (Generated with epplus library). I don't even know how to debug this part of the code. it's doing nothing.

Here's the error I'm getting in my browser:

Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Details: Error parsing near 'PKX��@ϖ�'.

Upvotes: 1

Views: 2739

Answers (2)

Ramesh
Ramesh

Reputation: 13266

I believe you are using an update panel. You cannot download a file when performing an Async Postback. Add the button which will download the file as Postback trigger for the update panel.

Upvotes: 2

Craig O
Craig O

Reputation: 852

You're on the right track. I think you're missing a couple of lines:

Response.Clear();
Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(pck.GetAsByteArray());
Response.Flush();
Response.Close();
Response.End();

Upvotes: 0

Related Questions