CoderWannaBe
.Net blog

Issues with PDF files

May 25, 2011 10:48 by bstinson

One of my applications streams PDF files (or any other type the user hapens to upload as an attachment).  Prior to the Adobe reader 9.4.2 update, the browser never had an issue
displaying the file.  After the update, the browser would no longer display the file, it would only allow the user to save the file to their hard drive and then open it up.  The dialog
that was displayed to the user had an interesting message at the bottom:

"The file you are downloading cannot be opened by the default program.  It is either corrupt or it has an incorrect file type.  As a security precaution it is recommended that you
cancel the download."

I finally found a post on the Adobe forums today that has many different solutions from many different developers that have worked around the issue, and I was also able to resolve
the issue for my application by using bits and pieces of the solutions provided in the forum post (http://forums.adobe.com/thread/391712).

Here was my code prior to the workaround:

    Dim DR As SqlDataReader = SqlHelper.ExecuteReader(Utils.HPSConn, CommandType.StoredProcedure, "hold_ticket_item_attachment_view_by_id", New SqlParameter("@id", Attachment_ID))
    If DR.Read Then
     Response.ClearContent()
     Response.ContentType = DR("mime_type").ToString
     'inline:  opens document in same browser window so that we do not have orphaned browser when done.
     'attachment:  opens in a seperate window, so we have an orphaned browser when done.
     Response.AppendHeader("Content-Disposition", "inline; filename=""" & DR("file_name").ToString & """")
     Dim file_data As Byte() = DR("file_data")
     Response.BinaryWrite(file_data)
    End If
    DR.Close()
    DR = Nothing

Here is the code that is now functioning as it should:

    Dim DR As SqlDataReader = SqlHelper.ExecuteReader(Utils.HPSConn, CommandType.StoredProcedure, "hold_ticket_item_attachment_view_by_id", New SqlParameter("@id", Attachment_ID))
                If DR.Read Then
                    Response.ClearHeaders()
                    Response.ClearContent()
                    'Content-Disposition:
                    'inline:  opens document in same browser window so that we do not have orphaned browser when done.
                    'attachment:  opens in a seperate window, so we have an orphaned browser when done.
                    Response.ContentType = DR("mime_type").ToString
                    Response.AppendHeader("Content-Disposition", "inline;filename=""" & DR("file_name").ToString & """")
                    Dim file_data As Byte() = DR("file_data")
                    Response.BinaryWrite(file_data)
                    Response.End()
            End If
                DR.Close()
                DR = Nothing

You'll notice the only 2 changes I made were to add the lines in bold.

Seems the changes (in Adobe Reader or the browser or whatever changed to cause this behavior) are "fixed" by making sure the response stream is clean and is properly terminated.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories:
Actions: E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

Comments

June 2. 2011 10:27

Gravatar

Usually I do not post on articles, but I would like to say that this article really forced me to do so! Thanks, really nice blog.

shooting games

June 3. 2011 05:37

Gravatar

Houses are expensive and not every person can buy it. But, home loans are created to support different people in such kind of hard situations.

credit loans

June 15. 2011 17:30

Gravatar

I never wanted to be a looser! Fortunately, once my friend proposed to buy a term paper. I don't feel disappointed anymore!

buy papers online

Comments are closed