HTTP响应头的内容配置的附件 [英] HTTP response header content disposition for attachments
问题描述
背景
编写一个XML文档浏览器的响应流,并导致浏览器显示另存为对话框。
问题
考虑以下下载()
方法:
HttpServletResponse的响应= GETRESPONSE();
BufferedWriter将体重=新的BufferedWriter(新OutputStreamWriter(
response.getOutputStream()));
字符串文件名=domain.xml中;
字符串MIMETYPE =新MimetypesFileTypeMap中()的getContentType(文件名)。
//打印应用程序/八位字节流
的System.out.println(MIMETYPE:+ MIMETYPE);
// response.setContentType(为text / xml;字符集= UTF-8);
response.setContentType(MIMETYPE);
response.setHeader(内容处置,附件;文件名=
+文件名);
bw.write(getDomainDocument());
bw.flush();
bw.close();
在Firefox中,XML内容显示在浏览器窗口。在IE 7中,不显示XML内容 - 你要查看的文档来源。既不情况是所希望的结果。
该网页使用下列$ C $下按钮:
< A4J:的commandButton行动=#{domainContent.download}值=创建域重新呈现=错误/>
这是生成的XML的不的开始与< XML版本=1.0>
,而XML内容酷似
<模式的xmlns =http://www.jaspersoft.com/2007/SL/XMLSchema版本=1.0>
<项目>
<项目说明=EDT类codedescriptionId =标签=EDT类codelabelId =RESOURCEID =as_pay_payrolldeduction.edtclass/>
< /项目>
<资源>
< jdbcTable的DataSourceID =JNDIID =as_pay_payrolldeductiontableName值=as_pay.payrolldeduction>
< fieldList中>
<字段id =payamountTYPE =java.math.BigDecimal中的/>
< / fieldList中>
< / jdbcTable>
< /资源>
< /架构>
更新#1
请注意的code以下行:
response.setHeader(内容处置,附件;文件名=+文件名);
更新#2
使用< A4J:的commandButton ... />
的问题;一个普通的< H:的commandButton ... />
按预期执行。使用< H:命令... />
prevents的< A4J:outputPanel ... />
从清凉的任何错误消息。
相关缝讯息。
Mime类型
以下MIME类型不触发另存为对话框:
-
应用程序/八位字节流
-
为text / xml
-
text / plain的
问题
什么变化,将导致 A4J:的commandButton
来触发一个另存为对话框,以便将提示用户保存XML文件( domain.xml中
)?
感谢你。
问题
在code有以下问题:
- 在Ajax调用(
< A4J:的commandButton ... />
)。不带附件的正常工作 - 创建输出的内容,必须先发生。
- 显示错误消息,也不能使用基于Ajax的
A4J
标签。
解决方案
- 修改
< A4J:的commandButton ... />
到< H:的commandButton ... />
。 - 更新源$ C $ C:
- 修改
bw.write(getDomainDocument());
到bw.write(文件);
- 添加
字符串文件= getDomainDocument();
到的try / catch
的第一行李>
- 修改
- 更改
< A4J:outputPanel ... />
(未显示)< H:消息showDetail =假 />
从本质上讲,删除相关的的commandButton
所有的阿贾克斯设施。它仍然有可能显示错误信息,并充分利用RichFaces的UI风格。
参考
- <一个href="http://stackoverflow.com/questions/4335849/using-a-commandbutton-in-a-jsf-page-to-download-a-file">Using一个命令在JSF页面中下载一个文件
- <一个href="http://www.$c$cranch.com/t/483892/JSF/java/when-commandButton-commandLink">http://www.$c$cranch.com/t/483892/JSF/java/when-commandButton-commandLink
- http://seamframework.org/135584.lace
Background
Write an XML document to a browser's response stream and cause the browser to display a "Save As" dialog.
Problem
Consider the following download()
method:
HttpServletResponse response = getResponse();
BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(
response.getOutputStream() ) );
String filename = "domain.xml";
String mimeType = new MimetypesFileTypeMap().getContentType( filename );
// Prints "application/octet-stream"
System.out.println( "mimeType: " + mimeType );
// response.setContentType( "text/xml;charset=UTF-8" );
response.setContentType( mimeType );
response.setHeader( "Content-Disposition", "attachment;filename="
+ filename );
bw.write( getDomainDocument() );
bw.flush();
bw.close();
In Firefox, the XML content is displayed in the browser window. In IE 7, the XML content is not displayed -- you have to view the document source. Neither situation is the desired result.
The web page uses the following code for the button:
<a4j:commandButton action="#{domainContent.download}" value="Create Domain" reRender="error" />
The XML that is generated does not start with <?xml version="1.0"?>
, rather the XML content resembles:
<schema xmlns="http://www.jaspersoft.com/2007/SL/XMLSchema" version="1.0">
<items>
<item description="EDT Class Code" descriptionId="" label="EDT Class Code" labelId="" resourceId="as_pay_payrolldeduction.edtclass"/>
</items>
<resources>
<jdbcTable datasourceId="JNDI" id="as_pay_payrolldeduction" tableName="as_pay.payrolldeduction">
<fieldList>
<field id="payamount" type="java.math.BigDecimal"/>
</fieldList>
</jdbcTable>
</resources>
</schema>
Update #1
Note the following line of code:
response.setHeader( "Content-Disposition", "attachment;filename=" + filename );
Update #2
Using <a4j:commandButton ... />
is the problem; a regular <h:commandButton .../>
performs as expected. Using the <h:commandBUtton .../>
prevents the <a4j:outputPanel .../>
from refreshing any error messages.
Related Seam Message.
Mime Type
The following mime types do not trigger the "Save As" dialog:
"application/octet-stream"
"text/xml"
"text/plain"
Question
What changes will cause the a4j:commandButton
to trigger a "Save As" dialog box so that the user is prompted to save the XML file (as domain.xml
)?
Thank you.
Problems
The code has the following issues:
- An Ajax call (
<a4j:commandButton .../>
) does not work with attachments. - Creating the output content must happen first.
- Displaying the error messages also cannot use Ajax-based
a4j
tags.
Solution
- Change
<a4j:commandButton .../>
to<h:commandButton .../>
. - Update the source code:
- Change
bw.write( getDomainDocument() );
tobw.write( document );
. - Add
String document = getDomainDocument();
to the first line of thetry/catch
.
- Change
- Change the
<a4j:outputPanel.../>
(not shown) to<h:messages showDetail="false"/>
.
Essentially, remove all the Ajax facilities related to the commandButton
. It is still possible to display error messages and leverage the RichFaces UI style.
References
- Using a commandButton in a jsf Page to download a file
- http://www.coderanch.com/t/483892/JSF/java/when-commandButton-commandLink
- http://seamframework.org/135584.lace
这篇关于HTTP响应头的内容配置的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!