tags:

views:

64

answers:

4

I am trying to display a PDF from database (byte[]) to user.

I am using code below to render PDF. It is giving me PDF as binary text as shown below. Instead of open in PDF application it is rendering PDF as text.

Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //Response.AddHeader("Content-Length", fileToDownload.Length.ToString());
        //Response.AddHeader("Content-Disposition", "inline; name=RemoteUserGuide.pdf");
        Response.AppendHeader("content-length", fileToDownload.Length.ToString());
        Response.ContentType = "application/pdf";
        Response.BinaryWrite(fileToDownload);
        Response.Flush();
        ////Response.Close();
        Response.End();

UPDATE: Just noticed, this application page is rendering PDF correctly in CHROME, but still displaying text in IE. (don't have FF3 on server to test). Probably its some browser issue?

Any Ideas? alt text

A: 

Make sure your MIME type for the .pdf extension in IIS (Internet Information Services) is set to "application/pdf".

Koen
He's setting the Content-type by hand.
Matti Virkkunen
Is that identical behavior than what IIS adds to the response?
Koen
+1  A: 

If you are sure, that your browser can view pdf, then you should use @PAGE ContentType="application/pdf" directive instead of Response.ContentType = "application/pdf";

Also you should use the Response.AddHeader("content-disposition", "filename=\"document.pdf\"" ); for setting filename

VMAtm
I think the OP is missing the filename call.
Chris Lively
Tried with filename still the same.
spt3210
A: 

As the others said, try setting the MIME type / header to application/pdf, if this doesn't work the last thing is closing your browser and reopening the same page, I had this problem before with FPDF (a PDF generation library in PHP), it looks like the browser cache the MIME of the page (php) and keep it forever, by closing the browser and reopening it, it will work #1.

Dominique
+3  A: 

Corrupted adobe install does this. reinstall Adobe Reader on the target (client) machine.

Sometimes the Adobe Installer crashes and does this.

ggonsalv
Issue was corrupted PDF installation. It's working fine after reinstalling. Thanks!
spt3210
Since we are short staffed, programmers man the help desk on days where there is less coverage. You get a complete picture, instead of hanging out in the programming ivory tower!
ggonsalv
I can't agree more, helpdesk would be really good experience for a developer. I was on Microsoft Support call few weeks back, and was amazed with kind of wide knowledge those guys have. It just cannot come with any training or reading books.
spt3210
It's called 'in the trenches'.
ggonsalv