只有在下载后才计算下载

Tom*_*len 10 c# asp.net download ashx

我们有这个代码供下载:

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);
Run Code Online (Sandbox Code Playgroud)

它工作正常,除了日志下载代码在下载开始后立即运行,而不是在下载完全按照我们的预期完成时.

有人可以解释为什么会这样,以及如何更改它以便它只在完成时记录?我们不想计算部分下载量.

Muh*_*han 5

博客文章与您的问题完全相同,也是一个解决方案.

Response.Buffer = false;
Response.TransmitFile("Tree.jpg");
Response.Close();
// logging here
Run Code Online (Sandbox Code Playgroud)