views:

52

answers:

1

I use the following code to send a .zip file to a customer, it works great for files around 60-80 Mb but when I try with larger files (300 Mb) customers report that the browser (both IE and FFX) just stops download progress after a few minutes (around (80Mb)) and nothing more happens.

I wonder if there could be a setup issue with IIS that stops the request handling thread to execute after a few minutes or if there is anything wrong with my code.

I could obviously find other ways to deliver .zip files but this issue is bothering me a bit and I want to get it working.

The code:

        Response.ContentType = "application/zip";

        Response.AddHeader("Content-Disposition", "attachment; filename=" + "hands.zip");
        var fi = new FileInfo(dld.Path);
        Response.AddHeader("Content-Length", fi.Length.ToString());
        const int buffercnt = 50000;
        var buffer = new byte[buffercnt];            
        using (var br = new BinaryReader( new StreamReader(dld.Path).BaseStream))
        {
            int read = br.Read(buffer, 0, buffercnt);
            while(read != 0)
            {
                Response.OutputStream.Write(buffer, 0, read);
                read = br.Read(buffer, 0, buffercnt);
                Response.Flush();
            }           
        }
    Response.Close();
    Response.End();
A: 

You can set the executionTimeout property in the httpRuntime tag in web.config to keep the code from being aborted.

Excuse me for saying it, but you have made a mess out of reading the file. You create a StreamReader only to have it open a FileStream for you, and you don't dispose of the StreamReader properly. Also, you use a BinaryReader, but you don't use any of the features of it, the Read method is available from the FileStream directly. So, skip the StreamReader and the BinaryReader and just create a FileStream yourself.

Guffa