J
Jerry Camel
I thought I had finally figured out how to control the downloads from my page. I was having issues with large files, but after much research I came up with the following code. The download seems to proceed without issue, but never terminiates. The byte count stops incrementing, but the dowload dialog never closes. (Until I hit cancel.) What's keeping the download from ending properly? I'm pretty close to finishing this app - any help is greatly appreciated. Thanks.
Dim fStream As FileStream
Dim bytesToGo As Long
Dim bytesRead As Long
Dim byteBuffer(1048576) As Byte
fStream = New FileStream("C:\DepotRoot\" & e.CommandArgument(), FileMode.Open, _
FileAccess.Read, FileShare.Read)
bytesToGo = fStream.Length
Response.BufferOutput = False
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment; filename=""" & _
dr("FileName") & """")
Response.Flush()
While (bytesToGo > 0)
If (Response.IsClientConnected) Then
bytesRead = fStream.Read(byteBuffer, 0, 1048576)
Response.OutputStream.Write(byteBuffer, 0, bytesRead)
Response.Flush()
bytesToGo -= bytesRead
Else
bytesToGo = -1
End If
End While
Response.Flush() 'This is kinda redundant, but it was worth a shot.
fStream.Close() 'Close the stream on the server side.
Response.End() 'Shouldn't this tell the browser to stop trying to download?
Thanks again!
- Jerry
Dim fStream As FileStream
Dim bytesToGo As Long
Dim bytesRead As Long
Dim byteBuffer(1048576) As Byte
fStream = New FileStream("C:\DepotRoot\" & e.CommandArgument(), FileMode.Open, _
FileAccess.Read, FileShare.Read)
bytesToGo = fStream.Length
Response.BufferOutput = False
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment; filename=""" & _
dr("FileName") & """")
Response.Flush()
While (bytesToGo > 0)
If (Response.IsClientConnected) Then
bytesRead = fStream.Read(byteBuffer, 0, 1048576)
Response.OutputStream.Write(byteBuffer, 0, bytesRead)
Response.Flush()
bytesToGo -= bytesRead
Else
bytesToGo = -1
End If
End While
Response.Flush() 'This is kinda redundant, but it was worth a shot.
fStream.Close() 'Close the stream on the server side.
Response.End() 'Shouldn't this tell the browser to stop trying to download?
Thanks again!
- Jerry