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?