File download is much easier in ASP.Net with the new WriteFile method in the Request object. Sending a file to the browser can be achieved just using a couple lines of code. To force the browser to display the Download dialog, you need to set the Resquest object's ContentType property to application/binary or application/zip and add an HTTP Request header called Content-Disposition with the following value: attachment; filename="filename" where filename is the default filename that will be displayed in the Download dialog box at the browser. The original code in C# is like:
1st version of code using Response.Flush();
Response.ContentType = "application/binary";
Response.AddHeader("Content-Disposition", "attachment; filname=" + MapPath(filename));
Response.WriteFile(MapPath(filename));
Response.Flush();
The downside of this code is that Response.Flush() on the code above would add the original html page file in the bottom of the download file, which we call it as extra junk. In order to avoid it, we can simply replace it with Response.End(); but you can't delete the file after downloading it (one of the reason why we need to delete it is the file is created on-the-fly and always needs to be up-to-date) because Response.End() ends the whole session. So we need to add "Content-Length", FileSize to chop off the extra junk. The following code also shows how to get the exact file size of the original file.
2nd version of code using Response.Flush(); with Content-Length and delete the original file after downloading
System.IO.FileInfo myFileInfo = new System.IO.FileInfo(MapPath(fileName));
Response.ContentType = "application/binary";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", myFileInfo.Length.ToString());
Response.WriteFile(MapPath(fileName));
Response.Flush();
System.IO.File.Delete(MapPath(fileName));
PHP/ASP version of code for forcing file downloads