Connection being closed when trying to send large zip file

0

What I'm trying to accomplish is to allow users to download multiple files from silverlight application. To do this I've decided to use DotNetZip library and ASP.NET handler that will take care of getting all files from database and sending them to client. It seemed like good idea and easy to implement. I've created simple handler, wrote all the code required and everything worked.

However for some reason, when I create zip file with many files, there is an issue. The remote host closed the connection. The error code is 0x800704CD. exception is being thrown when I try to write data to Response.

Private Sub MultipleFileDownload_Loaded(sender As Object, e As EventArgs) Handles Me.Load
    // initialize data and stuff

    _context.Response.Clear()
    _context.Response.BufferOutput = False
    Me.ZipElementsIntoOutputStream(elementsToDownload)
    _context.Response.End()
End Sub

Private Sub ZipElementsIntoOutputStream(elements As List(Of ElementImageFile))
    _context.Response.ContentType = "application/zip"
    Dim archiveName As String = String.Format("archive-{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HHmmss"))
    _context.Response.AddHeader("content-disposition", "attachment; filename=" + archiveName)

    Using zip As New ZipFile()
        For Each elementToDownload In elements.Where(Function(e) e IsNot Nothing AndAlso e.File IsNot Nothing)
            Dim fileName = Me.GetUniqueFileName(elementToDownload, zip)
            zip.AddEntry(fileName, elementToDownload.File)
        Next

        Using s As IO.MemoryStream = New IO.MemoryStream()
            zip.Save(s)
            s.Seek(0, IO.SeekOrigin.Begin)
            Dim buffer(10000) As Byte
            Dim length As Integer
            Dim dataToRead As Long
            dataToRead = s.Length

            While dataToRead > 0
                If (Me._context.Response.IsClientConnected) Then
                    length = s.Read(buffer, 0, 10000)
                    Me._context.Response.OutputStream.Write(buffer, 0, length)
                    Me._context.Response.Flush()

                    ReDim buffer(10000)
                    dataToRead = dataToRead - length
                Else
                    dataToRead = -1
                End If
            End While

            'zip.Save(_context.Response.OutputStream)
        End Using
    End Using
End Sub

As you can see I'm creating MemoryStream and sending small pieces of data to Response, as I've seen it shown as solution to similar problems, but this didn't help. Saving Zip file directly to Response is giving me exactly the same error.

BufferOutput property is set to False so it would start download immediately, but changing it to True does not change anything.

The zip file I'm trying to send is about 248 megabytes, and this gives me error. When I remove some elements and zip file is around 220 megabytes, everything seems to work fine.

Does anyone knows, what might be the reason for this behavior? How can I fix this, so sending zip files will not give me this error?

asp.net
ashx
dotnetzip
asked on Stack Overflow Jan 25, 2012 by Jarek

1 Answer

0

As it turns out, the problem was not in DotNetZip and ASHX handler. Problem was in very long query string passed to this handler. Internet Explorer can handle up to 2048 characters in query string. And unfortunately I passed longer string.

IE instead of giving me some kind of error to indicate the problem, created connection to server, and did not waited for my response but immediately closed connection, which was cause of my problems.

Fixing query string length problem fixed also this behavior and now downloading larger files works correctly.

answered on Stack Overflow Jan 27, 2012 by Jarek

User contributions licensed under CC BY-SA 3.0