I’ve got a problem when i tried to read some file in asp.net which is like 35mb large over the network. I also need this to be reliable since the file will be growing quickly within a few weeks. I tried to use my method to get the file and then when the file is 32mb in size it works fine but when it’s growing larger then it throws me an error of System.OutOfMemoryException in mscorlib.dll and it’s coming from this code “sr.ReadToEnd()”.
What i suspect was it’s because I’m reading the file which is too large to fit all in one big chunk of string. And i tried to fix my method which is reading the file in smaller piece of file and start to omit it to browser slowly. The fix is included in below:
Original File
Public Sub DishUpFile(ByVal filename As String) 'first map a path to the file Dim filepath As String = filename Using sr As System.IO.StreamReader = New System.IO.StreamReader(filepath) Dim buff As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(sr.ReadToEnd()) Response.Clear() Response.Buffer = True Response.ClearHeaders() Response.ContentType = "text/csv" Response.AddHeader("Content-Disposition","attachment;filename=AllMembers.csv") Response.BinaryWrite(buff) Response.End() End Using End Sub
Fix:
Public Sub DishUpCFile(ByVal filename As String) Dim nBytesRead As Integer = 0 Const mSize As Integer = 1024 Dim bytes As Byte() = New Byte(mSize - 1) {} 'Open or override a file in the local directory Dim fsFile As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read) Response.Clear() Response.Buffer = True Response.ClearHeaders() Response.ContentType = "text/csv" Response.AddHeader("Content-Disposition", "attachment; filename=AllMembers.csv") 'Read the first bit of content, then write and read all the content 'From the FromStream to the ToStream. nBytesRead = fsFile.Read(bytes, 0, mSize) While nBytesRead > 0 Response.OutputStream.Write(bytes, 0, nBytesRead) nBytesRead = fsFile.Read(bytes, 0, mSize) End While Response.OutputStream.Close() fsFile.Close() End Sub
Leave a Reply