Display a PDF in ASP.NET from a SQL Image field

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Try
            'declare variables
            Dim uuid As Guid
            Dim ByteArray As Byte()
 
            'we expect a GUID in the query string, like this: http://mysite.com/mypage.aspx?uuid=5955320C-5D7C-4D3A-8EAE-DD4A1D6AD94A
            'if it's not there, bail
            If Request("uuid") Is Nothing Then
                Response.Redirect("~/default2.aspx")
            End If
 
            'get the uuid
            uuid = New Guid(Request("uuid"))
 
            'query the database and get a class that contains the IMAGE field
            Dim oFPDocument As New DynData.FPDocument(uuid, App.ConnectionString)
 
            'if the data access failed, bail
            If Not oFPDocument.isPopulated Then
                Response.Redirect("~/default2.aspx")
            End If
 
            'populate our byte array
            ByteArray = oFPDocument.ImageBLOB
 
            'output the PDF to the browser
            Response.Clear()
            Response.Buffer = True
            Response.AddHeader("Content-Length", ByteArray.Length.ToString())
            Response.AddHeader("Content-Disposition", "inline; filename=PDF")
            Response.AddHeader("Expires", "0")
            Response.AddHeader("Pragma", "cache")
            Response.AddHeader("Cache-Control", "private")
            Response.ContentType = "application/pdf"
            Response.BinaryWrite(ByteArray)
            Response.Flush()
 
        Catch ex As ThreadAbortException
            'the response.redirects throw an error, but it's not really an error
        Catch ex As Exception
            'this is really an error, so handle it
            ErrorHandler.globalErrorHandler(ex, False)
 
        End Try
    End Sub

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences