locked
How to view a pdf file from the Database RRS feed

  • Question

  •  

    Anyone please help me.

     

    There is a content of the PDF file is stored in my tables's CONTENT field as BLOB datatype.

    My objective is to retrieve that file and view that file(as a PDF file opened) when i clicked on a button named as viewFile.

     

    Friday, October 5, 2007 9:18 AM

Answers

  • Hello,

    For retrieving from the DatabaseTable

     

    if you are using asp.net then the following code may help you

     

    SqlConnection cn1 = new SqlConnection("Connection String);

    cn1.Open();

    FbCommand cmd1 = new FbCommand("select content from table ", cn1);

     

     

    FileStream fs;

    BinaryWriter bw;

     

    int bufferSize = 100;

    byte[] outbyte = new byte[bufferSize];

    long retval;

    long startIndex = 0;

     

    FbDataReader dr1 = cmd1.ExecuteReader();

    while (dr1.Read())

    {

    fs = new FileStream(Path.GetTempPath() +filename , FileMode.OpenOrCreate, FileAccess.Write);

    bw = new BinaryWriter(fs);

    startIndex = 0;

    retval = dr1.GetBytes(0, startIndex, outbyte, 0, bufferSize);

    while (retval == bufferSize)

    {

    bw.Write(outbyte);

    bw.Flush();

    startIndex += bufferSize;

    retval = dr1.GetBytes(0, startIndex, outbyte, 0, bufferSize);

    }

    bw.Write(outbyte, 0, (int)retval - 1);

    bw.Flush();

     

    //if you are using asp.net then the following code may help you

    string file =path of your PDF file

     

    bw.Close();

    fs.Close();

    Response.Clear();

    Response.ContentType = "application/PDF";// "application/ms-word";

    Response.AddHeader("Content-Disposition", "attachment");

    Response.WriteFile(file);

    Response.Flush();

    Response.Close();

     

    Friday, October 12, 2007 8:59 AM

All replies

  • I am having the same problem. I have been researching for a good solution for it for the past week. It seems we have to know the path of the document to open it with System.Diagnostics.Process or display it with WebBrowser control. I am not satisfied with this solution. Does any one have some other ideas?

     

    Thursday, October 11, 2007 5:44 PM
  • How about you read the pdf from the server and regenerate it on the client.  Then you can open it
    Thursday, October 11, 2007 7:37 PM
  • Hello,

    For retrieving from the DatabaseTable

     

    if you are using asp.net then the following code may help you

     

    SqlConnection cn1 = new SqlConnection("Connection String);

    cn1.Open();

    FbCommand cmd1 = new FbCommand("select content from table ", cn1);

     

     

    FileStream fs;

    BinaryWriter bw;

     

    int bufferSize = 100;

    byte[] outbyte = new byte[bufferSize];

    long retval;

    long startIndex = 0;

     

    FbDataReader dr1 = cmd1.ExecuteReader();

    while (dr1.Read())

    {

    fs = new FileStream(Path.GetTempPath() +filename , FileMode.OpenOrCreate, FileAccess.Write);

    bw = new BinaryWriter(fs);

    startIndex = 0;

    retval = dr1.GetBytes(0, startIndex, outbyte, 0, bufferSize);

    while (retval == bufferSize)

    {

    bw.Write(outbyte);

    bw.Flush();

    startIndex += bufferSize;

    retval = dr1.GetBytes(0, startIndex, outbyte, 0, bufferSize);

    }

    bw.Write(outbyte, 0, (int)retval - 1);

    bw.Flush();

     

    //if you are using asp.net then the following code may help you

    string file =path of your PDF file

     

    bw.Close();

    fs.Close();

    Response.Clear();

    Response.ContentType = "application/PDF";// "application/ms-word";

    Response.AddHeader("Content-Disposition", "attachment");

    Response.WriteFile(file);

    Response.Flush();

    Response.Close();

     

    Friday, October 12, 2007 8:59 AM