Tech Off Thread

28 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

ASPX page to stream a PDF

Back to Forum: Tech Off
  • User profile image
    Red5

    All,
    I have used this code for the past 3-4 years and never had any problem with any version of .NET or Adobe Reader.  It is supposed to open a PDF in a new browser window.

    I copied this code to a new page in the same web application and it does not work.  To describe how it does not work; the resulting ASPX is just blank.  No PDF.  No Adobe toolbar.  Nothing.

    I've tried all sorts of variations on the code but no luck.  I tried adding a Response.End to the end of the code, but this threw an error. ???

    It still works fine in the original page though.  Very wierd.

    Can anyone provide some insight here? 


    ASPX code:  Just the @Page directive, nothing else


    VB Code: (Page_Load event)

    If IO.File.Exists(strReport) = False Then
       
    Response.Write("Cannot find file " & strReport)
    Else
       
    MyFileStream = New FileStream(strReport, FileMode.Open, FileAccess.Read)
       FileSize = MyFileStream.Length
       Dim  Buffer() As Byte
       ReDim Preserve Buffer(CInt(FileSize - 1))
       MyFileStream.Read(Buffer, 0, CInt(FileSize - 1))
       MyFileStream.Close()
       Response.BufferOutput =
    True
       
    Response.ClearContent()
       Response.ClearHeaders()
       Response.AddHeader("Content-Length", Buffer.Length.ToString)
       Response.AddHeader("content-disposition", "inline;filename=YourReport.pdf")
       Response.ContentType = "application/pdf"

       
    Response.BinaryWrite(Buffer)
       Response.Flush()
       Response.Clear()
    End If

  • User profile image
    stevo_

    Without looking into the code:

    http://www.fiddlertool.com/fiddler/

    Is a great tool for debugging the http traffic of your applications.

  • User profile image
    Red5

    stevo_ wrote:
    Without looking into the code:

    http://www.fiddlertool.com/fiddler/

    Is a great tool for debugging the http traffic of your applications.


    Thanks for the link to Fiddler.  I had heard of it but never bothered to try it.
    I tried it today and examined the streams from my two different web pages that stream PDF's.

    No difference at all.

    I'm still stumped.

  • User profile image
    jgillette

    The only obvious thing that comes to my mind, if the same page works at another site, is that your not referencing the PDF file correctly. Double check that the PDF file is in the same directory as your ASPX page (or update the code if it isn't).

  • User profile image
    littleguru

    jgillette wrote:
    The only obvious thing that comes to my mind, if the same page works at another site, is that your not referencing the PDF file correctly. Double check that the PDF file is in the same directory as your ASPX page (or update the code if it isn't).


    This should return a string, if not found.

  • User profile image
    PeterF

    How big is the PDF?
    Ints cannot hold big values

  • User profile image
    Red5

    There appears to be some confusion on some of the replies so let me make this a little clearer:

    Two ASPX pages in the same application:
    http://www.myroot.com/PDFPage1.aspx
    http://www.myroot.com/PDFPage2.aspx

    PDFPage1.aspx has existed for years and works beautifully.

    PDFPage2.aspx is a new copy of PDFPage1.aspx.  It does not work as expected

    The code that calls the PDF file and displays it is identical in the example code I provided.  There is a difference in that this code is inside different conditional logic blocks on the two pages.  That should not matter, but maybe someone has a different opinion on that topic.

    I tested this code multiple times with only one PDF document, so I can verify the code on both pages is being executed.  Both pages are attempting to load the exact same PDF document.  Page1 succeeds and Page2 fails.  The PDF I am using is 60K.

    Page1 has worked for PDF documents that range in size from a few K to over 50 MB in size.

    Whew.  I hope that makes it perfectly clear.  I hope someone can help me here.  My gut feeling is that the existence of conditional logic is confusing ASP.NET somehow.

  • User profile image
    Matthew van Eerde

    Maybe the conditional isn't working as you think it is.  Try outputting debug statements to the HTTP stream and see the code path that is actually being followed.

  • User profile image
    Red5

    Matthew van Eerde wrote:
    Maybe the conditional isn't working as you think it is.  Try outputting debug statements to the HTTP stream and see the code path that is actually being followed.


    See my previous post:

    I tested this code multiple times with only one PDF document, so I can verify the code on both pages is being executed.  Both pages are attempting to load the exact same PDF document.  Page1 succeeds and Page2 fails.  The PDF I am using is 60K.

    I know for a fact the conditional code is working because I put debug breaks in the proper locations on both files. 

    Sorry if that wasn't clear enough.

  • User profile image
    Matthew van Eerde

    This is odd:
    "tried it today and examined the streams from my two different web pages that stream PDF's."

    So, the PDF data is being streamed to the browser, but the browser isn't displaying it?

  • User profile image
    RichardRudek

    Red5 wrote:
    All,

    [snip]

    Can anyone provide some insight here? 

    [snip]

       Dim  Buffer() As Byte
       ReDim Preserve Buffer(CInt(FileSize - 1))
       MyFileStream.Read(Buffer, 0, CInt(FileSize - 1))

    [snip]



    I know you have cut'n'pasted, etc. But humor me (us):

    What happens if you don't reduce the file size (Highlighted in red, above). ie Read and Send the Last byte of the PDF?

    Also, I know very little about ASP.NET security contexts, etc, so I might be stearing you in the wrong direction here. But what's the ownership of aspx file(s) - ie compare the owners of both aspx files/resources (Security tab, advance, etc).

    Edit: I just read the VB.NET doco, and the ReDim was right (changed from VB6), but FileStream.Read is expecting a count. I'd probably change the ReDim anyway, just from a consisency/maintainability point of view, though.

    Oh, and I'm assuming that the posted code is a cut-down version, without the error/sanity checking... [A]

  • User profile image
    Red5

    RichardRudek wrote:
    I know you have cut'n'pasted, etc. But humor me (us):

    What happens if you don't reduce the file size (Highlighted in red, above). ie Read and Send the Last byte of the PDF?

    Also, I know very little about ASP.NET security contexts, etc, so I might be stearing you in the wrong direction here. But what's the ownership of aspx file(s) - ie compare the owners of both aspx files/resources (Security tab, advance, etc).

    Edit: I just read the VB.NET doco, and the ReDim was right (changed from VB6), but FileStream.Read is expecting a count. I'd probably change the ReDim anyway, just from a consisency/maintainability point of view, though.

    Oh, and I'm assuming that the posted code is a cut-down version, without the error/sanity checking...


    A valid point.
    You found a legitimate and glaring error on not reading in the last byte.
    Unfortunately, changing it has no effect.  It still does not work in the new page, but works in the old page (I changed both).

    How it has worked for years is a small miracle maybe.  If the last byte is always 00, which I am seeing consistently in many PDFs, then maybe it has been sliding by for that reason.

    The security contexts on both files are identical.

    I have included only the code that does the lifting for simplicity.  I did not include the Try-Catch stuff.

  • User profile image
    RichardRudek

    OK. So we now need to start questioning assumptions.

    First of all, this is the same web browser (PC), for both tests. Right ?

    I was just reading up on "Content-Disposition" RFC (note the casing). Theoretically, the Response.Clear*() stuff in your code should be clearing the responses, but have you checked ?. See RFC2616, which specifically talks about this header extension. Also note that the RFC states that the filename param should be quoted.

    Check/try [Rahul's blog]. He has some other less verbose methods of do stuff like this. Also, there's one person there complaining about this type of thing not working from a code-behind - obviously, there has to be some per-page property or setting  causing issues (viewstate?).

    PS: I'm not a dotNET developer, so I probably shouldn't be putting my bib in, here. Although I think I'm at the bottom of my suggestions barrel.

  • User profile image
    stevo_

    Are both servers running the same environment?

    For example, 1.1 application on a 2.0 server doesn't run entirely the same as a 1.1 application on a 1.1 server.

    At least- this is what I have noticed.

    It may be worth trying a different method (or debugging out some simple stuff like jpegs) to see if you can pinpoint the anomaly.

  • User profile image
    Red5

    RichardRudek wrote:
    OK. So we now need to start questioning assumptions.

    First of all, this is the same web browser (PC), for both tests. Right ?
    "Yes"

    I was just reading up on "Content-Disposition" RFC (note the casing). Theoretically, the Response.Clear*() stuff in your code should be clearing the responses, but have you checked ?. See RFC2616, which specifically talks about this header extension. Also note that the RFC states that the filename param should be quoted.
    "Interesting, I will try putting quotes around the file name."

    Check/try [Rahul's blog]. He has some other less verbose methods of do stuff like this. Also, there's one person there complaining about this type of thing not working from a code-behind - obviously, there has to be some per-page property or setting  causing issues (viewstate?).
    "I have read Rahul's stuff previously and unfortunately we have to support Adobe Reader 5.0, which does not qualify for his examples. I am going to try inline script instead of code behind.  Maybe that will trick it into working."

    PS: I'm not a dotNET developer, so I probably shouldn't be putting my bib in, here. Although I think I'm at the bottom of my suggestions barrel.
    "I appreciate you taking the time"



    stevo_ wrote:
    Are both servers running the same environment?
    "There is only one server, again it is two pages in the SAME application"

    For example, 1.1 application on a 2.0 server doesn't run entirely the same as a 1.1 application on a 1.1 server.
    "It is .NET 2.0, clean install"

    At least- this is what I have noticed.

    It may be worth trying a different method (or debugging out some simple stuff like jpegs) to see if you can pinpoint the anomaly.
    "I will try both pages with a JPEG and see what happens."

  • User profile image
    Red5

    I tried a content type of "image/jpg" in both pages.  Works correctly and the same in both pages.  A jpg of my choice was output in the browser windows.

    I tried putting the vb code behind as inline script.
    Works on the first page correctly, but not in page2.  Again, page2 is being a real stinker.

    I think I'm about to give up.

    In all the examples so far, Adobe Reader 5.0 is my default reader.

    On the bright side, when this is put on a public web server, and I connect from home where my version of Adobe Reader is 7.0, it works fine.  Go figure.  Hopefully not many users are still using Adobe 5.0.

    It still doesn't explain why page1 works with Adober Reader 5.0 and page2 does not.

    Thanks for the feedback everyone as I learned a couple of points here. 
    If anyone has a brainstorm feel free to add it. 
    I will check back periodically.

  • User profile image
    RichardRudek

    Yeah, it looks like the only way your going to figure this out is to examine the response, byte by byte, as seen by the client (web browser). I'd be saving the Byte stream, converted to text (toString), then use WinDIff to examine the differences between the responses of both pages.

    From memory, Acrobat implements web browser integration as a [Browser Helper Object] (BHO). But I'm not sure that sending the PDF to the browser via Content-Disposition and ContentType will cause the browser to use the Acrobat BHO. I suspect Internet Explorer will be using ShellExecute instead, like from Windows (file) Explorer. That'll also mean that there will likely be security issues, as well (Trusted sites list, etc).

    As for differences between Acrobat 5 and 7, I'm not sure. But I do know that when you change the "Browser integration" option in v7, it kicks off an instance of Installer to add or remove the Acrobat BHO. In v5, that was not the case, and the only way I used to remove the Acrobat BHO was to use SpyBot's Advanced tools, which has a tool to do this - it's been a while, though.


  • User profile image
    Harlequin

    Red5 wrote:

    MyFileStream.Read(Buffer, 0, CInt(FileSize - 1))


    Try this for your Read:
    MyFileStream.Read(Buffer, 0, 65536)

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.