为什么IE在GWT中不显示PDF文件下载对话框?

Why doesn't IE show file download dialog for PDF with GWT Window.open()?

本文关键字:PDF 文件下载 对话框 显示 IE GWT 为什么      更新时间:2023-09-26

我们正在为客户生成PDF文档,并使用Servlet为其提供服务。以下代码适用于Firefox, Chrome和Opera,但不适用于任何版本的IE。弹出窗口只在IE中闪烁,但什么也没发生。但是,我们可以通过从IE的地址栏向servlet发出直接请求来显示文件下载对话框。我们已经尝试了几种ContentTypes (application/download, application/x-download等)

客户端代码:
String URL = "/files/pdf?pdfId=" + getPdfId();
Window.open(URL, "_blank", "");

Servlet将pdf作为byte[]:

byte[] bytes = getFileAsByteArray();
BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(bytes));
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
response.setHeader("Content-Disposition", "attachment; filename=document.pdf");
response.setContentType("application/octet-stream");
response.setContentLength(bytes.length);
byte[] buffer = new byte[8192];
for (int length = 0; (length = in.read(buffer)) > 0;) {
    out.write(buffer, 0, length);
}
out.flush();
out.close();

对此有什么想法吗?

试试这个:

Content-Disposition: inline

代替:

Content-Disposition: attachment;filename=document.pdf

试试这个,然后说点什么发现如果我使用内联,那么我不应该使用filename=document.pdf,这在IE中不起作用。(其他浏览器忽略)

你可以在这里阅读:http://indiwiz.com/2009/03/11/forcing-http-download/

可能是由于pdf插件问题。链接到adobe网站获取更多详细信息

如果你不能解决问题-试着改变你的问题!尝试发送压缩的pdf文件或其他内容。它应该能帮助你找到问题的根源。如果一切都能用zip文件,你就不需要发送pdf-file

尝试:

Window.open(GWT.getHostPageBaseURL()+URL,"","");

这可能会工作!我用的是:

com.google.gwt.user.client.Window.open(GWT.getHostPageBaseURL()+URL, "", "");   

它运行正常