WriteFile, BinaryWrite, OutputStream.. all not working

R

Random

I have tried all the Response methods I can think of (WriteFile,
BinaryWrite, OutputStream) to write the byte array of a pdf file to the
response. The result looks like it's trying, it comes up as a blank PDF
file (without the Adobe toolbar that normally comes up). Here is the code I
am using...

'...fPDF is my Byte array
'...I've already verified it is valid by writing the array to a physical
file ("result.pdf") and opening it manually
'----------------------------------------------
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"

'...trying WriteFile
Response.WriteFile("~/xml/result.pdf")

'...trying BinaryWrite
Response.BinaryWrite(fPDF)

'...trying OutputStream
Response.OutputStream.Write(fPDF, 0, fPDF.Length)
Response.Flush()
Response.Close()
'--------------------------------------------

Is it possibly because the requested page has the *.aspx extension? If it
is, is there a way around this?
 
J

Jc Morin

Hi,

I would suggest to add the content disposition to make sure the browser
actually knows it's a binary download.

I've just did the test and it works!

Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment; filename=file.pdf")
Response.WriteFile("file.pdf")

Hope that help.
 
R

Random

Well, that got the pdf to download fine. The problem now is, I'm getting
the "download or save" dialog and the file is opening in Acrobat. I'd like
it to display in the browser window.
 
M

Matt Berther

Hello Rick Strahl [MVP],

Actually, wouldnt the correct Content-Disposition be:

Response.AddHeader("Content-Disposition", "inline;filename=file.pdf");
 
R

Random

So far the only thing that works is using the "content-disposition" set for
"attachment". I think the problem is that since the requested page has an
..aspx extension, the browser doesn't want to display it as a PDF file. As I
said before, all I get when I try it without adding the header is a blank
page with a very fine border around it, as if it was a blank PDF file.
There is no extra toolbar that IE will add in when opening a PDF file
directly. The PDF file itself will open in IE. I've done that
independently, and using the "content-disposition" header, the file does
open in Adobe Acrobat.

Does anyone have a workaround for this?
 
G

Guest

I am seeing the same issue. Using the code below, taken from Article ID
307603 on MSDN, the browser appears to load the acrobat activex but does not
display it. The page is blank.

The code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Set the appropriate ContentType.
Response.ContentType = "Application/pdf"
'Get the physical path to the file.
Dim FilePath As String = MapPath("acrobat.pdf")
'Write the file directly to the HTTP output stream.
Response.WriteFile(FilePath)
Response.End()
End Sub

Perhaps a browser security issue of some sort?
 
R

Random

I found the above suggestions worked... that adding in the header did
properly notify the browser of the intended MIME type.

Response.AddHeader("content-disposition", "attachment; filename=file.pdf")

As an interesting followup, what I did NOT like about this behavior is that
because it described the PDF as an attachment, it ended up opening within
Acrobat, and not in the browser window. However, this was my test page. I
moved my code over to my in-use page, where the user clicked on a 'Create
PDF' link button to generate a PDF version of the page. I didn't change the
original Page_Load event, but just put the code into the Click event for the
LinkButton. I changed the AddHeader line to this...

Response.AddHeader("content-disposition", "inline; filename=file.pdf")

And the PDF opened within the browser, just like I originally wanted!!

I hope this information helps some people.
 
G

Guest

The content header does not work for me (XP Pro SP2, IE6, Acrobat 6).

I have found that if I redirect the PDF load page to a page where the uri
ends in .pdf (i.e change loadpdf.aspx to loadpage.aspx?File=Adobe.pdf), it
will display as expected. Also noticed that if there are any webcontrols on
the form, the PDF will not load.
 
R

Random

Have you tried using methods other than WriteFile? Since my PDF files are
being created dynamically using XSL-FO files, I don't ever have a physical
file to deal with, but instead write a byte array to the output stream.

What is the request/response process you are actually expecting? If you
have physical files for the user to request, can you just display a
hyperlink to the file? Or if you are determining which file to use through
submit parameters, can you do a Server.Transfer or Response.Redirect to that
URL?

Also, loking again at your code, instead of...

Response.End()

try...

Response.Flush()
Response.Close()

Give me a better idea of what you are trying to accomplish and I will try to
help you more. WebControls shouldn't interfere with what you are doing
since you aren't going to display anything on the page.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top