ASP Forum
[Ask] [.net] Problem with flusing data
alex2016 | Posted 0:59am 15. April 2008 Server Time |

Hi all,

I am working on some WAP site using ASP.net where user can download song/picture/etc. The code that I use is attached below. Tested and worked well, but as traffic getting higher, I'm facing memory leakage where my IIS got a very huge chunk of memory allocated.

Is there something wrong with the code? Please advice, thank.


Regards,


Alex
===

code:

Public Shared Sub DownloadContent(ByVal strFileLocation As String, Optional ByVal strFileContentType As String = "application/octet-stream")
Dim iStream As System.IO.Stream

' Buffer to read 10K bytes in chunk:
Dim buffer(10000) As Byte

' Length of the file:
Dim length As Integer

' Total bytes to read:
Dim dataToRead As Long

' Identify the file to download including its path.
Dim filepath As String = strFileLocation

' Identify the file name.
Dim filename As String = System.IO.Path.GetFileName(filepath)

Try
    ' Open the file.
    iStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, _
   IO.FileAccess.Read, IO.FileShare.Read)

    ' Total bytes to read:
    dataToRead = iStream.Length

    HttpContext.Current.Response.ContentType = strFileContentType
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" & filename)

    ' Read the bytes.
    While dataToRead > 0
' Verify that the client is connected.
If HttpContext.Current.Response.IsClientConnected Then
    ' Read the data in buffer
    length = iStream.Read(buffer, 0, 10000)

    ' Write the data to the current output stream.
    HttpContext.Current.Response.OutputStream.Write(buffer, 0, length)

    ' Flush the data to the HTML output.
    HttpContext.Current.Response.Flush()

    ReDim buffer(10000) ' Clear the buffer
    dataToRead = dataToRead - length
Else
    'prevent infinite loop if user disconnects
    dataToRead = -1
End If
    End While
Catch ex As Exception
    ' Trap the error, if any.
    HttpContext.Current.Response.Write("Error : " & ex.Message)
Finally
    If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
    End If
End Try

End Sub


Reply to Post [Ask] [.net] Problem with flusing data



Back to Forum Page