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?

4

1 回答 1

0

事实证明,问题不在于DotNetZip处理ASHX程序。问题在于传递给此处理程序的查询字符串很长。Internet Explorer最多可以处理查询字符串中的 2048 个字符。不幸的是,我传递了更长的字符串。

IE而不是给我某种错误来指示问题,而是创建了与服务器的连接,并且没有等待我的响应而是立即关闭了连接,这是我的问题的原因。

修复查询字符串长度问题也修复了这种行为,现在下载更大的文件可以正常工作。

于 2012-01-27T07:13:34.863 回答