10 marzo 2009

File download support

http://in.relation.to/Bloggers/Daniel

Web applications often need to provide a link for the user to download a file, such as an expense report. Seam 2.1.2.CR1 makes the task of serving a file to the user easy and RESTful using a combination of the s:resource and s:download tags. But before we can use these tags, we need to do some simple configuration.

The s:resource tag uses Seam’s DocumentStore to serve (or push) documents. You'll need to configure this servlet in to your web.xml:

web.xml


Document Store Servlet
org.jboss.seam.document.DocumentStoreServlet


Document Store Servlet
/seam/docstore/*

Alone in a page, s:resource will act as the file download provider by defining where to get the file data and meta-data. You use it in place of HTML (not inside of HTML) since the intent is to serve the document and not a web page. Thus, the page template should look something like this.

resources.xhtml

 xmlns:s="http://jboss.com/products/seam/taglib"
data="#{fileResourceProvider.data}"
contentType="#{fileResourceProvider.contentType}"
fileName="#{fileResourceProvider.fileName}" />

  • data is the actual file data. It may be a java.util.File, an InputStream or a byte[].
  • contentType (e.g., image/jpeg)
  • filename (e.g., someFile.jpg) which will likely be displayed in the user's file download prompt

Other attributes are described in the Seam reference documentation.


No hay comentarios: