spt3210
spt3210

Reputation: 45

PDF coming as text on browser

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

Upvotes: 2

Views: 720

Answers (4)

ggonsalv
ggonsalv

Reputation: 1262

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

Sometimes the Adobe Installer crashes and does this.

Upvotes: 3

Dominique
Dominique

Reputation: 908

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.

Upvotes: 0

VMAtm
VMAtm

Reputation: 28345

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

Upvotes: 1

Koen
Koen

Reputation: 3686

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

Upvotes: 0

Related Questions