【发布时间】:2011-07-13 19:59:40
【问题描述】:
背景
将 XML 文档写入浏览器的响应流,并使浏览器显示“另存为”对话框。
问题
考虑以下download() 方法:
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();
在 Firefox 中,XML 内容显示在浏览器窗口中。在 IE 7 中,不显示 XML 内容——您必须查看文档源。这两种情况都不是我们想要的结果。
网页对按钮使用以下代码:
<a4j:commandButton action="#{domainContent.download}" value="Create Domain" reRender="error" />
生成的 XML不以 <?xml version="1.0"?> 开头,而是 XML 内容类似于:
<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>
更新 #1
注意以下代码行:
response.setHeader( "Content-Disposition", "attachment;filename=" + filename );
更新 #2
使用<a4j:commandButton ... /> 是问题所在;一个普通的<h:commandButton .../> 按预期执行。使用<h:commandBUtton .../> 可以防止<a4j:outputPanel .../> 刷新任何错误消息。
相关Seam Message.
Mime 类型
以下 mime 类型不会触发“另存为”对话框:
"application/octet-stream""text/xml""text/plain"
问题
哪些更改会导致a4j:commandButton 触发“另存为”对话框,从而提示用户保存 XML 文件(作为domain.xml)?
谢谢。
【问题讨论】:
标签: java ajax jboss richfaces attachment