저는 사용자가 Silverlight 응용 프로그램에서 여러 파일을 다운로드 할 수 있도록하려고합니다. 이를 위해 나는 DotNetZip
라이브러리와 ASP.NET
핸들러를 사용하여 데이터베이스에서 모든 파일을 가져 와서 클라이언트에게 보냄으로써 처리하기로 결정했습니다. 그것은 좋은 생각과 구현하기 쉬운 것 같았다. 나는 간단한 핸들러를 만들고, 필요한 모든 코드를 작성했으며 모든 것이 잘 작동했다.큰 zip 파일을 보내려고 할 때 연결이 닫힙니다.
그러나 많은 이유로 zip 파일을 만들 때 어떤 문제가 있습니다. Response
에 데이터를 쓰려고하면 The remote host closed the connection. The error code is 0x800704CD.
예외가 발생합니다.
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 메가 바이트이며 이로 인해 오류가 발생합니다. 일부 요소를 제거하고 zip
파일이 약 220 메가 바이트 인 경우 모든 것이 정상적으로 작동하는 것 같습니다.
누구든지이 동작에 대한 이유를 알고 있습니까? 어떻게하면이 문제를 해결할 수 있습니까? Zip 파일을 보내면이 오류가 발생하지 않습니다.