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

其他回答

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

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

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

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

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

我今天就遇到了这个问题,我的问题是我的Content-Disposition标签设置错误。 对于pdf和application/x-zip-compressed,应该将其设置为内联而不是附件。

所以要设置你的头文件,Java代码应该是这样的:

...
String fileName = "myFileName.zip";
String contentDisposition = "attachment";
if ("application/pdf".equals(contentType)
    || "application/x-zip-compressed".equals(contentType)) {
    contentDisposition = "inline";
}
response.addHeader("Content-Disposition", contentDisposition + "; filename=\"" + fileName + "\"");
...