Chrome浏览器无法从asp.net页面中显示PDF浏览器、页面、Chrome、asp

2023-09-04 04:11:52 作者:别赔罪

在.aspx页面中有一个.pdf文件,像这样:<嵌入SRC =HTTP://.../ShowPdf.aspx ID = 1类型=应用程序/ PDF> 。 Chrome浏览器只显示加载的形象,并挂而不显示PDF(铬PDF浏览器和Adobe插件两个不工作)。其他浏览器打开PDF文件。任何想法?

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?

推荐答案

我有这个问题也和我多次调研后,终于解决了这个问题。

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

我发现的是, HTTP 响应头应具有以下实体头:

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

内容长度:文件大小

如果没有指定这个头,Web服务器就会把默认的:

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

内容长度:分块

我不知道为什么谷歌浏览器有这个问题,因为正如你所说,在其他浏览器如IE或Firefox,他们提供/打开正确的PDF文件。

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.

以下是我的code,我来解决这个问题。它的工作在我的情况,现在谷歌浏览器显示我的Web应用程序的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!

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

我希望我帮助你。

I hope that I had helped you.

下面是一些参考资料,我发现有用: 与Chrome和 PDF处理程序问题;火狐 What's在HTTP头中的内容长度字段?

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