Tech Off Post

Single Post Permalink

View Thread: ASPX page to stream a PDF
  • 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