题
我想要完成的是允许用户从 silverlight 应用程序下载多个文件。为此,我决定使用 DotNetZip
图书馆和 ASP.NET
处理程序将负责从数据库获取所有文件并将它们发送到客户端。这似乎是个好主意并且很容易实现。我创建了简单的处理程序,编写了所需的所有代码并且一切正常。
但是由于某种原因,当我创建包含许多文件的 zip 文件时,出现了问题。 The remote host closed the connection. The error code is 0x800704CD.
当我尝试写入数据时抛出异常 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
正如你所看到的,我正在创建 MemoryStream
并将小块数据发送到 Response
, ,正如我所看到的,它被证明是类似问题的解决方案,但这没有帮助。保存 Zip
直接归档到 Response
给了我完全相同的错误。
BufferOutput
属性设置为 False
所以它会立即开始下载,但将其更改为 True
不会改变任何东西。
这 zip
我尝试发送的文件大约为 248 MB,这给了我错误。当我删除一些元素并 zip
文件大约 220 MB,一切似乎都工作正常。
有谁知道,这种行为的原因可能是什么?我该如何解决这个问题,这样发送 zip 文件就不会出现此错误?
解决方案
事实证明,问题并不在于 DotNetZip
和 ASHX
处理程序。问题在于传递给该处理程序的查询字符串非常长。 Internet Explorer
最多可以处理查询字符串中的 2048 个字符。不幸的是我传递了更长的字符串。
IE
而不是给我某种错误来指示问题,而是创建了与服务器的连接,并且没有等待我的响应而是立即关闭了连接,这是我的问题的原因。
修复查询字符串长度问题也修复了此行为,现在下载更大的文件可以正常工作。