locked
Retrieve PDF from Sql Database into a web browser RRS feed

  • Question

  • User898474388 posted

    I am trying to retrieve an PDF from Sql database and display the PDF in a web browser. I am getting a blank page in the new web browser..When I change the Response.ContentType = "images/pdf"; it pulls up an image correctly. But when I trie to change the Response.ContentType = "application/PDF"; there is nothing but a blank page. Here is my code..can someone help me please!!

    protected void Page_Load(object sender, EventArgs e)

    {

    try

    {

    System.
    Int32 _ImgID = System.Convert.ToInt32(Request.QueryString["ImgID"]);System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Connection String");

     

    System.
    String SqlCmd = "SELECT ACTUAL_IMAGE_PDF2 FROM DBO.RIMS_TEST_IMAGES WHERE IMAGEID = @IMAGEID";

     

    System.Data.SqlClient.SqlCommand SqlCmdObj = new System.Data.SqlClient.SqlCommand(SqlCmd, con);

    SqlCmdObj.Parameters.Add("@IMAGEID", System.Data.SqlDbType.Int).Value = _ImgID;

    con.Open();

     

    System.Data.SqlClient.
    SqlDataReader SqlReader = SqlCmdObj.ExecuteReader();

    SqlReader.Read();

    System.Web.
    HttpContext.Current.Response.ContentType = "image/jpeg"; // For pdf, I use "application/pdf"

    System.Drawing.Image _image = System.Drawing.Image.FromStream(new System.IO.MemoryStream((byte[])SqlReader["ACTUAL_IMAGE_PDF2"]));

    _image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

    }

    catch (System.Exception Ex)

    {

     

    System.Web.
    HttpContext.Current.Trace.Write(Ex.Message.ToString());

    }

    }

    }

    Thursday, October 4, 2007 12:18 PM

Answers

  • User-627724879 posted

    Sorry I missed this line of code at the beginning:

    Dim sDir As String = System.Configuration.ConfigurationManager.AppSettings("PDFRoot")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2007 2:33 PM

All replies

  • User-627724879 posted

    PDF is not an image, you need to use response.BinaryWrite (pdfByteArray), where pdfByteArray is the bytes that represent the PDF document.

    Thursday, October 4, 2007 12:38 PM
  • User898474388 posted

    This is what I changed my my code to:

     I am still getting a blank page.

    SqlReader.Read();

    System.Web.
    HttpContext.Current.Response.ContentType = "application/pdf";

    Response.BinaryWrite((byte[])SqlReader["ACTUAL_IMAGE_PDF2"]);

    Thursday, October 4, 2007 12:59 PM
  • User-627724879 posted

    This is what I use to stream a real PDF file from the file system. You may want to try to store the bytes from the database as a real file and see if you can open it in either Acrobat or FoxIt to see if it has really been stored correctly in the database.

     

    'The next two sections are from Scott Hanselman's blog, but seem to be out of date, since it was posted in 2003

    'http://www.hanselman.com/blog/InternetExplorerAndTheMagicOfMicrosoftKBArticleQ293792.aspx

    If InStr(1, context.Request.UserAgent, "contype") > 0 Then

    'Just send the mime/type

    context.Response.ContentType = "application/pdf"

    context.Response.End()

    Exit Sub

    End If

     

    Dim Language

    Language = context.Request.ServerVariables("HTTP_ACCEPT_LANGUAGE")

    If Language = "" Then

    context.Response.Clear()

    context.Response.ContentType = "application/pdf"

    context.Response.AddHeader("Last-modified", "Mon, 01 Sep 1997 01:03:33 GMT")

    context.Response.Status = "304 Not Modified"

    context.Response.End()

    Exit Sub

    End If

     

    context.Response.Cache.SetExpires(DateTime.MinValue)

    context.Response.ContentType = "application/pdf"

     

    If File.Exists(sDir & sDocument) Then

    context.Response.WriteFile(sDir & sDocument)

    context.Response.End()

    End If

    Thursday, October 4, 2007 2:32 PM
  • User-627724879 posted

    Sorry I missed this line of code at the beginning:

    Dim sDir As String = System.Configuration.ConfigurationManager.AppSettings("PDFRoot")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2007 2:33 PM