Read PDF from a Database and Open it into a Frame

IHttpHandler is aowsome. You read your file in the buffer and just send it to src as if it is a named file, so no need to read and save a temporary file in client’s PC.

iframePDF1.Attributes.Add(“src”, “ShowPDF.ashx?pID=” & Request.QueryString(“pID”))

ShowPDF.ashx

<%@ WebHandler Language=”vb” %>

Imports System

Imports System.Configuration

Imports System.Web

Imports System.IO

Imports System.Data

Imports System.Data.SqlClient

Public Class ShowPDF

    Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim imgName As String

        If Not context.Request.QueryString(“pID”) Is Nothing Then

            imgName = Convert.ToString(context.Request.QueryString(“pID”))

        Else

            Throw New ArgumentException(“No parameter specified”)

        End If

        context.Response.ContentType = “application/pdf”

        Dim strm As Stream = ShowLawsImage(imgName)

        If Not strm Is Nothing Then

            Dim buffer As Byte() = New Byte(4095) {}

            Dim byteSeq As Integer = strm.Read(buffer, 0, 4096)

            Do While byteSeq > 0

                context.Response.OutputStream.Write(buffer, 0, byteSeq)

                byteSeq = strm.Read(buffer, 0, 4096)

            Loop

            context.Response.BinaryWrite(buffer)

        End If

    End Sub

    Public Function ShowLawsImage(ByVal imgName As String) As Stream

        Dim conn As String = ConfigurationManager.ConnectionStrings(“SqlServerConnection”).ConnectionString

        Dim connection As SqlConnection = New SqlConnection(conn)

        Dim sql As String = “SELECT imageLaw FROM Laws WHERE ID = @ID”

        Dim cmd As SqlCommand = New SqlCommand(sql, connection)

        cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue(“@ID”, imgName)

        connection.Open()

        Dim img As Object = cmd.ExecuteScalar()

        Try

            Return New MemoryStream(CType(img, Byte()))

        Catch

            Return Nothing

        Finally

            connection.Close()

        End Try

    End Function

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable

        Get

            Return False

        End Get

    End Property

End Class

PDF.aspx.vb

Imports System.Data

Imports System.Data.SqlClient

Partial Class PDF

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        iframePDF1.Attributes.Add(“src”, “ShowPDF.ashx?pID=” & Request.QueryString(“pID”))

    End Sub

End Class

PDF.aspx

<body>

    <form id=”form1″ runat=”server”>

      <div>

         

</div>

    </form>

</body>

 

 Ahmed.

Advertisements

About Ahmed

Software craftsman, programmer, developer, system/business analyst, DBA and PM.
This entry was posted in Databases, Programming, SQL-Server, VB.NET and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s