Chrome not displaying PDF from asp.net page

11,474

Solution 1

I had this problem too and I finally solved it after several researches.

What I found is that the HTTP response header should have the following entity-header:

content-length: file size

Without specify this header, the web server will put the default:

content-length: chunked

I don't know why Google Chrome has this issue, because as you said, in other browsers like IE or Firefox, they render/open correctly the PDF file.

Following is my code that I used to solve this problem. It worked in my case and now Google Chrome is displaying the PDF file of my web application!

    System.IO.FileInfo fileInfo;
    fileInfo = My.Computer.FileSystem.GetFileInfo(fullPathToFile);
    Response.AddHeader("Content-Length", fileInfo.Length.ToString());

I hope that I had helped you.

Here are some references that I found useful:
PDF Handler Problem with Chrome & Firefox
What's the “Content-Length” field in HTTP header?

Solution 2

Thank you very much, I had the same problem, and it was the solution. I was using a report viewer, but i needed to show it in pdf automatically, and it was working correctly, but when i used Chrome, it had the same issue, when i want to save the file, it was saved with extension .ASPX and not with .pdf.

I share my code with the solution:

                string deviceInfo = "";
                string[] streams = null;
                string mimeType = null;
                string encoding = null;
                string fileNameExtension = null;
                Warning[] war = null;
                Byte[] bytes = this.ReportViewer.LocalReport.Render("PDF", deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out war);
                System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
                Response.Clear();
                Response.ContentType = "Application/pdf";
                Response.AddHeader("Content-Length", bytes.Length.ToString());
                Response.AddHeader("Content-disposition", "inline; filename= NombreReporte");
                Response.BinaryWrite(ms.ToArray());
                Response.Flush();
                Response.End(); 

Solution 3

If you also have the problem showing a PDF in Firefox, I found this:

"You need to specify the content type so the browser knows what file it is downloading / showing inline....like so:"

     Response.ContentType = "application/pdf"
     Response.AddHeader("Content-Disposition", "inline;filename=YourFileName.pdf")

It´s worked for me, I hope for you too and help you.

Source: http://forums.asp.net/t/1784151.aspx

Share:
11,474
meggar
Author by

meggar

iOS, Objective-C, Swift, Cocoa, Python

Updated on June 30, 2022

Comments

  • meggar
    meggar almost 2 years

    An .aspx page has a .pdf file in it, like this: <embed src="http://.../ShowPdf.aspx?id=1" type="application/pdf">. Chrome just shows a "Loading" image and hangs without displaying the pdf (chrome pdf viewer and adobe plugin both don't work). Other browsers open the pdf. Any ideas?