I am downloading files with ASP.NET Web Forms in the click event of a LinkButton as follows:
var file = new FileInfo(filePath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString(CultureInfo.InvariantCulture));
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
The code works, however when the file is a .txt
it, in addition to its original content, comes with all the HTML of the current page. Does anyone know how to solve this?