使用Chrome 12.0.742.112,如果我重定向以下头:

HTTP/1.1 302 Found 
Location: http://0.0.0.0:3000/files/download.zip
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
X-Ua-Compatible: IE=Edge
X-Runtime: 0.157964
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)
Date: Tue, 05 Jul 2011 18:42:25 GMT
Connection: Keep-Alive

如果遵循,将返回以下头文件:

HTTP/1.1 200 OK 
Last-Modified: Tue, 05 Jul 2011 18:18:30 GMT
Content-Type: application/zip
Content-Length: 150014
Server: WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)
Date: Tue, 05 Jul 2011 18:44:47 GMT
Connection: Keep-Alive

Chrome不会重定向,也不会改变前一页,它只会在控制台中报告以下警告:

资源解释为文档,但使用MIME类型application/zip传输。

这个过程在Firefox中正常工作,如果我打开一个新选项卡并直接访问http://0.0.0.0:3000/files/download.zip,在Chrome中也可以正常工作。是我做错了什么,还是这是Chrome的bug/怪癖?


当前回答

我在提供PDF文件(MIME类型应用程序/ PDF)时遇到了这个问题,并通过设置Content-Disposition头来解决它,例如:

Content-Disposition: attachment; filename=foo.pdf

希望这能有所帮助。

其他回答

我通过在链接中添加target="_blank"来解决这个问题。 有了这个,chrome打开一个新标签和加载PDF没有警告,甚至在响应模式。

您可以在<a>标记中指定HTML5下载属性。

<a href="http://example.com/archive.zip" download>Export</a>

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-download

Chrome 61版本再次出现此问题。但它似乎是固定的Chrome 62。

我有一个重写规则如下

RewriteRule ^/ShowGuide/?$ https://<website>/help.pdf [L,NC,R,QSA]

在Chrome 61中,PDF不能打开,在控制台显示消息

"Resource interpreted as Document but transferred with MIME type application/pdf: "

我们尝试在重写规则中添加mime类型,但它没有帮助。

RewriteRule ^/ShowGuide/?$ https://<website>/help.pdf [L,NC,R,QSA, t:application/pdf]

我已经把我的Chrome浏览器更新到最新的62版本,它又开始显示PDF了。但是消息仍然在控制台中。

在所有其他浏览器中,它都可以正常工作。

在我的情况下,文件名太长,并得到了相同的错误。一旦缩短到200个字符以下工作正常。(上限可能是250?)

尝试下面的代码,我希望这将为您工作。

var Interval = setInterval(function () {
                if (ReportViewer) {
                    ReportViewer.prototype.PrintReport = function () {
                        switch (this.defaultPrintFormat) {
                            case "Default":
                                this.DefaultPrint();
                                break;
                            case "PDF":
                                this.PrintAs("PDF");
                                previewFrame = document.getElementById(this.previewFrameID);
                                previewFrame.onload = function () { previewFrame.contentDocument.execCommand("print", true, null); }
                                break;
                        }
                    };
                    clearInterval(Interval);
                }
            }, 1000);