附件的 HTTP 响应标头内容处置 [英] HTTP response header content disposition for attachments

查看:33
本文介绍了附件的 HTTP 响应标头内容处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 XML 文档写入浏览器的响应流,并使浏览器显示另存为"对话框.

Write an XML document to a browser's response stream and cause the browser to display a "Save As" dialog.

考虑以下 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 内容——您必须查看文档源.这两种情况都不是想要的结果.

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" />

生成的 XML <?xml version="1.0"?> 开头,而 XML 内容类似于:

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>

更新 #1

注意以下代码行:

Update #1

Note the following line of code:

response.setHeader( "Content-Disposition", "attachment;filename=" + filename );

更新 #2

使用 是问题所在;常规 按预期执行.使用 可以防止 刷新任何错误消息.

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.

相关Seam 消息.

以下 MIME 类型不会触发另存为"对话框:

The following mime types do not trigger the "Save As" dialog:

  • 应用程序/八位字节流"
  • 文本/xml"
  • 文本/纯文本"

哪些更改会导致 a4j:commandButton 触发另存为"对话框,以便提示用户保存 XML 文件(作为 domain.xml)?

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)?

谢谢.

推荐答案

问题

代码存在以下问题:

Problems

The code has the following issues:

  • Ajax 调用 () 不适用于附件.
  • 必须首先创建输出内容.
  • 显示错误消息也不能使用基于 Ajax 的 a4j 标签.
  • 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.
  1. 更改为 .
  2. 更新源代码:
  1. Change <a4j:commandButton .../> to <h:commandButton .../>.
  2. Update the source code:
  1. bw.write(getDomainDocument());改为bw.write(document);.
  2. try/catch的第一行添加String document = getDomainDocument();.
  1. Change bw.write( getDomainDocument() ); to bw.write( document );.
  2. Add String document = getDomainDocument(); to the first line of the try/catch.

  • (未显示)更改为 .
  • Change the <a4j:outputPanel.../> (not shown) to <h:messages showDetail="false"/>.
  • 本质上,删除所有与 commandButton 相关的 Ajax 工具.仍然可以显示错误消息并利用 RichFaces UI 样式.

    Essentially, remove all the Ajax facilities related to the commandButton. It is still possible to display error messages and leverage the RichFaces UI style.

    这篇关于附件的 HTTP 响应标头内容处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆