Topic: Streaming binary file data to web page
Share/Save/Bookmark
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        If Request.QueryString.Item("action") <> "NoStream" Then
            Dim obj As New MyApplication.MyObject
            Dim data As MyApplication.Object.MyObjectImage
            Dim redirect As Boolean = False
 
            Try
                data = obj.GetFileData(Request.QueryString.Item("id"), Request.QueryString.Item("version"))
                Response.ClearContent()
                Response.ClearHeaders()
                Response.Clear()
                If data.ContentType = "text/xml" Or data.ContentType = "text/html" Then
                    Response.ContentType = "application/msword"
                Else
                    Response.ContentType = data.ContentType
                End If
                Response.ContentType = data.ContentType
                Response.AddHeader("Content-Length", data.Bytes.Length.ToString)
                Response.AddHeader("Content-Disposition", "inline; filename=" & data.MyObjectName)
                'Response.AddHeader("Content-Disposition", "attachment; filename=" & data.MyObjectName)
                If data.Size > 0 Then 'a text MyObject with no content inside
                    Response.BinaryWrite(data.Bytes)
                End If
            Catch ex As Exception
                redirect = True
            Finally
                obj = Nothing
                data = Nothing
            End Try
 
            Response.End()
 
            If redirect Then
                Response.Redirect("ErrorPage.aspx")
            End If
 
        Else
            Response.Write("Cannot stream this file type")
        End If
    End Sub