Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

opening pdf with .ashx generic handler

Posted on 2009-12-17
8
Medium Priority
?
1,984 Views
Last Modified: 2012-05-08
I used the code below in a .ashx generic handler page to display a pdf file.
The code below was retrieving a file that was saved in the database as a varbinary(max) datatype.

I want to do this again and the only difference this time is the file is saved in the database as an image datatype.  

Do I have to alter the code to retrieve data stored as an image datatype???

     public void ProcessRequest(HttpContext ctx)
        {
           
            string id = ctx.Request.QueryString["id"];

            SqlConnection con = new SqlConnection("connection string here");
            SqlCommand cmd = new SqlCommand("SELECT File FROM tblFiles where FileId = @fileID", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@fileId", id);
       
        con.Open();
        byte[] pict = (byte[])cmd.ExecuteScalar();

        con.Close();
        try
        {
            ctx.Response.ContentType = HttpContext.Current.Session["fileExtension"].ToString();
            ctx.Response.OutputStream.Write(pict, 0, int.Parse(pict.Length.ToString()));
        }
        catch
        {
           
        }
0
Comment
Question by:LATurk
  • 6
  • 2
8 Comments
 

Author Comment

by:LATurk
ID: 26076741
This is the error I am getting
Unable to cast object of type 'System.Int32' to type 'System.Byte[]'.

on this line of code
byte[] pict = (byte[])cmd.ExecuteScalar();
0
 
LVL 10

Expert Comment

by:anv
ID: 26078139
what is the data type of File Field in database?
0
 
LVL 10

Accepted Solution

by:
anv earned 2000 total points
ID: 26078146
Also how are you storing the PDF file in DB?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LATurk
ID: 26080125
the pdf file is stored in an Image datatype.
0
 

Author Comment

by:LATurk
ID: 26080394
Actually, I'm closer now.  The actual select statement I was using selected 2 fields.  An id field and then the image field.  I took out the id field so i'm only selecting the image and I no longer get that error.

Now it opens in the ashx page but it's all garbled.
0
 

Author Comment

by:LATurk
ID: 26080535
I got it to work using the below:
Does it look good to you?  Anything important I missed?

       public void ProcessRequest(HttpContext ctx)
        {
           
            string id = ctx.Request.QueryString["id"];
           
            SqlConnection con = new SqlConnection("connection string");
            SqlCommand cmd = new SqlCommand("SELECT File FROM tblFiles where FileId = @fileID", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@fileId", id);
       
        con.Open();
        byte[] pict = (byte[])cmd.ExecuteScalar();
           
        con.Close();
       
        try
        {
           
            ctx.Response.ContentType = HttpContext.Current.Session["application/pdf"].ToString();
            ctx.Response.BinaryWrite(pict);
        }
           
        catch
        {
           
        }
        }
       
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
0
 

Author Comment

by:LATurk
ID: 26081757
I have one more question.

The users will click a button to go to the ashx url to view a pdf.
I have the users computer set to use Adobe Professional to open the pdf.
If the user adds comments to the pdf, is it possible to save the new copy back to the database?

I've done this in a windows application but not a web application.
0
 

Author Closing Comment

by:LATurk
ID: 31667599
I am going to award points even though I ended up finding the solution.  After reading your questions, I ended up googling it in a different way which led me to the solution.  I will post my second question in a new post.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question