We have this code which serves a download:
public class downloadRelease : IHttpHandler {
public void ProcessRequest (HttpContext context) {
-- snip --
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + OriginalFileName);
context.Response.WriteFile(Settings.ReleaseFileLocation + ActualFileName);
// Log download
Constructor.VersionReleaseDownload.NewReleaseDownload(ActualFileName);
It works fine, except that the log download code runs as soon as the download starts seemingly, not when the download has fully completed as we expect.
Can someone explain why this is, and how to change it so it only logs when it's completed? We don't want to count partial downloads.
This blog post has exactly same issue as yours and a solution too.
Response.Buffer = false;
Response.TransmitFile("Tree.jpg");
Response.Close();
// logging here
The write response is an asynchronous process. It is managed by the container for the application. In your case the .NET/ASP.net run time is handling it. If you want to find out when the last chunk was sent you'll have to have some kind of callback/event on that [coming from the container/runtime]. In Java its the application server that gets this information [Glassfish, Tomcat etc]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With