Problem when sending a file to the client

  • Thread starter frenchie seb via .NET 247
  • Start date
F

frenchie seb via .NET 247

Hi !

here's the code of the function I use to send a file to the client :


Private Function SendFile(ByVal fich As String, ByVal nom As String) As Boolean

Try

Dim oldCT As String = Response.ContentType

Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=""" & nom & """")

Dim fi As IO.FileInfo = New IO.FileInfo(Server.MapPath("fichiers\") & fich)
If Not fi.Exists Then
Return False
Exit Function
End If

Dim fs As IO.FileStream = fi.OpenRead
Dim tab(fs.Length) As Byte
fs.Read(tab, 0, fs.Length)
fs.Close()
Response.BinaryWrite(tab)

Response.Flush()
Response.Close()

Response.Clear()
Response.ContentType = oldCT

Return True

Catch ex As Exception

Return False

End Try

End Function


When I call it, the transfer works fine, the file is ok, but the web application stops responding to the client. When the client clicks a link or tries to do something on the page, nothing happens and the status bar indicates "Error on page"

Has someone an idea ?

Thanks in advance
 
M

Martin Marinov

Why do you have Response.Close() method ?
Look what is written about this method in MSDN : Closes the socket
connection to a client.
I think you should remove it from there

Regards
Martin

frenchie seb via .NET 247 said:
Hi !

here's the code of the function I use to send a file to the client :


Private Function SendFile(ByVal fich As String, ByVal nom As String) As Boolean

Try

Dim oldCT As String = Response.ContentType

Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=""" & nom & """")

Dim fi As IO.FileInfo = New
IO.FileInfo(Server.MapPath("fichiers\") & fich)
If Not fi.Exists Then
Return False
Exit Function
End If

Dim fs As IO.FileStream = fi.OpenRead
Dim tab(fs.Length) As Byte
fs.Read(tab, 0, fs.Length)
fs.Close()
Response.BinaryWrite(tab)

Response.Flush()
Response.Close()

Response.Clear()
Response.ContentType = oldCT

Return True

Catch ex As Exception

Return False

End Try

End Function


When I call it, the transfer works fine, the file is ok, but the web
application stops responding to the client. When the client clicks a link or
tries to do something on the page, nothing happens and the status bar
indicates "Error on page"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top