1

I'm trying to create a file handler for users to download files when their filenames are clicked on a web page. I've implemented this a few times without issues, but I'm currently getting an error which I can't get my head around.

Code:

protected void btnViewFile_Click(object sender, EventArgs e)
{
    var btnViewFile = sender as LinkButton;
    Response.Clear();
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + btnViewFile.CommandArgument.ToString());
    Response.WriteFile(Server.MapPath(btnViewFile.CommandArgument));
    Response.End();
}

If I look at the browser console, I can see:

Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

No exceptions appear to be thrown in the code, the requested file is converted into the correct full path; and I've tried quite a few different things - clearing headers manually, flushing before ending, giving a more explicit content-type header, using AddHeader instead of AppendHeader, using TransmitFile rather than WriteFile, and quite a bit more.

Any ideas?

jacob21
  • 171
  • 4
  • 16
  • Have you checked [PageRequestManagerParserErrorException: The message received from the server could not be parsed](http://stackoverflow.com/questions/11221033/sys-webforms-pagerequestmanagerparsererrorexception-the-message-received-from-t) ? – Matias Cicero Sep 01 '15 at 12:27
  • I can't actually see it being thrown anywhere. – jacob21 Sep 01 '15 at 12:35
  • 1
    Your problem is with some client side code. Do you have any client side code? – mason Sep 01 '15 at 13:22
  • 1
    You're attempting download file from an update panel. That you cannot do (when m$ client engine tries to parse response as html to see what part of page was updated he see binary file). You have to disable AJAX for file download. See for example this http://stackoverflow.com/questions/5461525/download-feature-not-working-within-update-panel-in-asp-net – Ondrej Svejdar Sep 01 '15 at 13:38
  • There's no client-side scripting on the page. – jacob21 Sep 01 '15 at 15:05
  • 1
    The download control isn't in an update panel. Should've added to the original post that it is being recognised as a postback fine. – jacob21 Sep 01 '15 at 15:06

1 Answers1

0

In case anyone else comes across this situation, the problem was that I was registering it as a postback control in ScriptManager, not an async postback control.

D'oh!

jacob21
  • 171
  • 4
  • 16