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

其他回答

我今天在Chrome 30.0.1599.66版本的node.js / express.js应用程序中遇到了同样的问题。

标题是正确的,表达设置他们正确地自动,它在其他浏览器中工作,如所示,把html 5的“下载”属性不解决,什么解决它是进入chrome高级设置,并勾选框“询问在下载前保存每个文件的位置”。

在此之后,没有“资源解释为文档....”的错误报告,因为在这个问题的标题中,所以它似乎是我们的服务器代码是正确的,这是Chrome,在控制台错误地报告错误时,它被设置为自动保存文件到一个位置。

当我在iframe中分配src="image_url"时,我遇到了这个问题。 iframe似乎将其解释为文档,但事实并非如此。这就是为什么它会显示警告。

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

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-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 + "\"");
...