Trying to read files stored as Varbinary(max) on my asp.net page.
Database schema is:
id int
fileName varchar(100)
fileSize varchar(20)
fileContent varbinary(MAX)
dateAdded smalldatetime
addedBy varchar(20)
fileMimeType varchar(25)
Code is:
Imports System.IO
Partial Class submission_viewfile
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Connect to the database and bring back the image contents & MIME type for the specified picture
Dim myConnection As SqlConnection = New SqlConnection(Configuratio
nManager.A
ppSettings
("Connecti
onString")
)
Const SQL As String = "SELECT * FROM Documents WHERE id = @id"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWi
thValue("@
id", Request.QueryString("id"))
myConnection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader
If myReader.Read Then
Dim data As Byte() = myReader("fileContent")
Using st As Stream = New MemoryStream(data)
Dim dataLengthToRead As Long = st.Length
If myReader("fileMimeType").T
oString() = "text/plain" Then
Response.ContentType = myReader("fileMimeType").T
oString()
Else
Response.ContentType = myReader("fileMimeType").T
oString()
Response.AddHeader("Conten
t-Disposit
ion", "attachment; filename=""" + myReader("fileName") + """")
End If
While dataLengthToRead > 0 AndAlso Response.IsClientConnected
Context.Response.BinaryWri
te(data)
End While
Response.Flush()
Response.Close()
End Using
Response.[End]()
End If
myReader.Close()
myConnection.Close()
End Sub
End Class
Page freezes and won't display file. Any way to easily read these files in a web browser?
Start Free Trial