简体   繁体   English

Chrome无法从asp.net页面显示PDF

[英]Chrome not displaying PDF from asp.net page

An .aspx page has a .pdf file in it, like this: <embed src="http://.../ShowPdf.aspx?id=1" type="application/pdf"> . .aspx页面中有一个.pdf文件,如下所示: <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). Chrome只显示“正在加载”图片并挂起而不显示pdf(chrome pdf viewer和adobe插件都不起作用)。 Other browsers open the pdf. 其他浏览器打开pdf。 Any ideas? 有任何想法吗?

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: 我发现HTTP响应头应该有以下实体头:

content-length: file size

Without specify this header, the web server will put the default: 如果不指定此标头,Web服务器将设置默认值:

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. 我不知道为什么谷歌Chrome有这个问题,因为正如你所说,在其他浏览器,如IE或Firefox,他们正确地渲染/打开PDF文件。

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! 它适用于我的情况,现在谷歌浏览器显示我的网络应用程序的PDF文件!

    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 PDF处理程序Chrome和Firefox的问题
What's the “Content-Length” field in HTTP header? HTTP标头中的“Content-Length”字段是什么?

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. 我正在使用报表查看器,但我需要自动以pdf格式显示它,并且它工作正常,但是当我使用Chrome时,它有同样的问题,当我想保存文件时,它以扩展名保存.ASPX而不是.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(); 

If you also have the problem showing a PDF in Firefox, I found this: 如果您在Firefox中显示PDF的问题,我发现:

"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 资料来源: http//forums.asp.net/t/1784151.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM