I have a simple aspx page where is in the codebehind I am creating a zip file and streaming it back. Everything worked fine while I kept all my code inside of page_load method. Now I am executing the code on a button click. After download Windows cannot open the zip saying it is invalid. Here is my code:
<body style="height: 100%; overflow: hidden" onload="return onBodyLoad();">
<script>
function onBodyLoad() {
document.getElementById("ExportBtn").click();
}
</script>
protected void ExportBtn_OnClick(object sender, EventArgs e)
{
// prepare zip
.....
// send zip back
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=" + fileToDownload);
Response.ContentType = "application/zip";
Response.BinaryWrite(File.ReadAllBytes(zipPath));
}
The same code wihtout a button click runs fine from the page_load which currently looks like this:
protected void Page_Load(object sender, EventArgs e)
{
}
Found the problem. All I needed is Response.End(); as the last statement.