views:

54

answers:

2

I have two files at the same location but the big one, when is about to finish download, gives an error (both in IE and Firefox).

I use the following code:

public static void DownloadZipFile (string filename, bool notifyMe)
{
    HttpContext context = HttpContext.Current;
    HttpServerUtility server = context.Server;
    bool ok = false;
    try
    {
        string file = string.Format ("~/contents/licensing/members/downloads/{0}", filename);
        string server_file = server.MapPath (file);

        HttpResponse response = context.Response;
        //response.BufferOutput = false;
        response.ContentType = "application/zip";
        string value = string.Format ("attachment; filename={0}", filename);
        response.AppendHeader ("Content-Disposition", value);
        FileInfo f = new FileInfo (server_file);
        long size = f.Length;
        response.TransmitFile (server_file, 0, size);
        response.Flush ();
        ok = true;
        response.End ();
    }
    catch (Exception ex)
    {
        Utilities.Log (ex);
    }
    finally
    {
        if (ok && notifyMe)
            NotifyDownload (filename);
    }
}

Any ideas?

A: 

Response.End() calls Response.Flush(). Try removing the Flush call.

matt-dot-net
I did it on purpose; I wanted to set the "ok" flag to true before "response" go away, and after flushing contents. Anyway, this does not solve the problem. Jim above made a suggestion that helped. However, thank you for the comment.
ileon
+1  A: 

The solution to this problem is to add the line:

response.AddHeader("Content-Length",size.ToString());

before the call to TransmitFile (). The credits go to Jim Schubert (see his comment above).

ileon