I have observed that when a file is downloaded with the following code:
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=~/test.pdf");
HttpContext.Current.Response.WriteFile("~/test.pdf");
HttpContext.Current.ApplicationInstance.CompleteRequest();
We get a file larger than if the code below is used:
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=~/test.pdf");
HttpContext.Current.Response.WriteFile("~/test.pdf");
HttpContext.Current.Response.End();
The larger size is because in the first case is added, after the end of the file, the source code of the page from which the file is downloaded. So all downloaded files are increased with a fixed number of bytes equal to the length in bytes of source code. In the case of a PDF, when open it with Adobe Reader, those extra bytes are interpreted as a change and when close prompted to save the changes, after which the pdf recovers its smaller size. If the files are compared with an hexdecimal editor warns that are identical but of different sizes and can be seen at the end, after the EOF mark, the bytes of page source code.
The same is true if, instead of a physical file, use a MemoryStream and send the array of bytes by Response.BinaryWrite(), for example.
What can be the cause of this behavior and how can it be corrected?