我们正在为我们的客户生成 PDF 文档并使用 Servlet 为他们提供服务。 以下代码适用于 Firefox、Chrome 和 Opera,但不适用于任何版本的 IE。弹出窗口仅随 IE 闪烁,但没有任何反应。但是,我们可以通过从 IE 中的地址栏直接请求 servlet 来显示文件下载对话框。 我们尝试了几种 ContentType(应用程序/下载、应用程序/x-download 等)
客户端代码:
String URL = "/files/pdf?pdfId=" + getPdfId();
Window.open(URL, "_blank", "");
Servlet 以 byte[] 形式提供 pdf:
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/
关于java - 为什么 IE 不显示带有 GWT Window.open() 的 PDF 文件下载对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6674982/