使用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/怪癖?


当前回答

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

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);

其他回答

您可以在<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

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

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);

我得到这个错误,因为我正在从我的文件系统服务。一旦我开始与一个http服务器chrome可以找出它。

在请求头中,您发送了Content-Type: text/html,这意味着您希望将响应解释为html。现在即使服务器发送PDF文件给你,你的浏览器也会将其理解为HTML。这就是问题所在。我正在寻找可能的原因。:)

这个问题

我也有类似的问题。 在js中获得消息

资源解释为文档,但以MIME类型text/csv传输

但我也在chrome控制台收到消息

混合内容:“https://my-site/”网站已加载 通过安全连接,但文件在 “https://my-site/Download?id = 99 a50c7b 通过不安全连接重定向。这个文件应该是 通过HTTPS服务。此下载已被阻止

这里说你需要使用一个安全的连接(但scheme在消息中已经是https了,奇怪…)

问题是href文件下载构建在服务器端。这个href在我的例子中使用了http。

解决方案

所以我在构建href文件下载时将scheme更改为https。